【问题标题】:How can I get my rotating platform script working in Unity?如何让我的旋转平台脚本在 Unity 中工作?
【发布时间】:2021-08-28 16:17:33
【问题描述】:

我目前正在以初学者的身份制作 2D 游戏。我想添加一个围绕中心旋转的平台。我已经在脚本中拥有了所有东西,但是隐藏平台的中心和顺时针旋转根本不起作用,所以我想问是否有人可以改进我的脚本。我还想让它像在统一资产商店的高级平台 2D 资产中一样,就像在屏幕截图上一样。所以你可以选择你的平台和中心。

谢谢!

截图:Advanced Platforms 2D Asset

当前脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotating_Platform : MonoBehaviour
{
    [SerializeField]
    Transform rotationCenter;
    public GameObject rotationObject;

    [SerializeField]
    float rotationRadius = 2f, angularSpeed = 2f;
    float posX, posY, angle = 0f;
    private float rotZ;
    public bool ClockwiseRotation;

    // Update is called once per frame
    void Update()
    {
        //Rotation
        posX = rotationCenter.position.x + Mathf.Cos(angle) * rotationRadius;
        posY = rotationCenter.position.y + Mathf.Sin(angle) * rotationRadius;
        transform.position = new Vector2(posX, posY);
        angle = angle + Time.deltaTime * angularSpeed;

        if (angle >= 360f)
            angle = 0f;

        //Clockwise Rotation
        if (ClockwiseRotation == false)
        {
            rotZ += Time.deltaTime * angularSpeed;
        }
        else
        {
            rotZ += -Time.deltaTime * angularSpeed;
        } 
    }

    public bool HideRotationCenter;
    private void Loading_Initial_Parameters()
    {  
      //Hiding Rotation Center
      if (HideRotationCenter)
      {
         if (rotationCenter.GetComponent<SpriteRenderer>() != null)
         {
             rotationCenter.GetComponent<SpriteRenderer>().enabled = false;
         }

         else
         {
            Debug.LogWarning("Rotation Center (" + rotationCenter.name + ") does NOT HAVE a SpriteRenderer Component to hide");
         }
      }

         else
         {
            //---Sprite renderers or Mesh renderers keep visible
         }


    }
}

【问题讨论】:

  • 3 个问题。 1.- 如果您使用transform.position = new Vector2(posX, posY); 在旋转中设置平台位置,那么rotZ 是什么? 2.-“顺时针旋转根本不起作用”是什么意思?是逆时针旋转工作还是平台不动?结果如何?控制台中的任何错误? 3.- 您是否将Rotating_Platform 脚本附加到平台游戏对象?
  • @rustyBucketBay 1. rotZ 可能根本不重要,因为我粘贴了顺时针旋转并隐藏了中心。 2. 是的,顺时针旋转有效,逆时针旋转无效。并且控制台没有错误 3. 不,它附加到一个空的 GameObject 以使其排列更清晰。

标签: c# unity3d


【解决方案1】:

为什么中心对象没有被隐藏

您的中心对象没有被隐藏的原因是因为Loading_Initial_Parameters() 方法不会在任何地方被调用。您可以通过在 Start() 方法中调用它来解决此问题。

为什么顺时针旋转不起作用

您的ClockwiseRotation 设置无法正常工作的原因是angle 的值不管它都朝着同一个方向发展。您可以通过添加 if else 语句来解决此问题,该语句在一种情况下会减去值,在另一种情况下会添加它们。

修改后的代码

using UnityEngine;

public class Rotating_Platform : MonoBehaviour
{
    [SerializeField]
    private Transform rotationCenter;

    public GameObject rotationObject;

    [SerializeField]
    private float rotationRadius = 2f, angularSpeed = 2f;

    public bool ClockwiseRotation;
    public bool HideRotationCenter;

    private float posX, posY, angle = 0f;

    private void Start()
    {
        Loading_Initial_Parameters();
    }

    private void Update()
    {
        // Rotation
        posX = rotationCenter.position.x + Mathf.Cos(angle) * rotationRadius;
        posY = rotationCenter.position.y + Mathf.Sin(angle) * rotationRadius;
        rotationObject.transform.position = new Vector2(posX, posY);

        float angularMovement = Time.deltaTime * angularSpeed;

        if (ClockwiseRotation)
            angle -= angularMovement;
        else
            angle += angularMovement;

        if (angle >= 360f)
            angle = 0f;
    }

    private void Loading_Initial_Parameters()
    {
        if (rotationCenter.TryGetComponent<SpriteRenderer>(out var rotationCenterSpriteRenderer))
        {
            rotationCenterSpriteRenderer.enabled = !HideRotationCenter;
        }
        else
        {
            Debug.LogWarning(
                $"Rotation Center ({rotationCenter.name}) does NOT HAVE " +
                $"a SpriteRenderer Component to hide/show");
        }
    }
}

【讨论】:

  • 谢谢,但一切都被隐藏了,即使我取消选中该框。
  • @JonathanXD12 如果是这种情况,那么可能是由于场景本身已经禁用了GameObject 或渲染组件。
  • @JonathanXD12 以防万一,更新了答案。该脚本现在应该根据您的布尔值启用/禁用SpriteRenderer。确保GameObject 本身已启用。否则,请随时查看gameObject.SetActive() 方法作为component.enabled 的替代方法。
  • 不,这太奇怪了,SpriteRenderer 在每个启用的对象上。但可能是脚本无法正常工作,因为我将它附加到了一个空的 GameObject 上,如下图所示:imgur.com/a/mQOOKFE
  • 嗯。该脚本似乎没有在任何地方使用rotationObject。相反,它使用transform 中的GameObject。更新了答案 - 替换了以下行:transform.position = new Vector2(posX, posY);.
猜你喜欢
  • 2021-07-26
  • 2021-07-25
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多