【问题标题】:How can I add a list in the Editor for my rotating platforms script in Unity如何在编辑器中为 Unity 中的旋转平台脚本添加列表
【发布时间】:2021-07-25 20:41:46
【问题描述】:

我目前正在制作 2D 游戏,并添加了一个围绕中心旋转的平台。现在我想要,您可以选择要围绕中心旋转多少个平台。就像在带有电路点的屏幕截图上一样。我对编码不是很感兴趣,请问您可以将其添加到我当前的脚本中吗?

谢谢!

截图: circuit_points list

当前脚本:

using UnityEngine;

public class Rotation_Object_System : 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");
        }
    }
}

【问题讨论】:

    标签: c# unity3d rotation


    【解决方案1】:

    我认为一些更改将使您可以使用。

    1. 将公共字段添加为列表或数组。这将允许您在 Unity 的检查器中添加/删除元素。
    2. 更新您的更新代码以循环遍历数组/列表中的每个项目并将位置更改应用于每个项目。
    3. 您可能需要添加一些代码来单独跟踪每个旋转位置...您已经在跟踪单个游戏对象的角度...您需要将其更新为数组/列表... 1您正在处理的每个游戏对象的项目。

    【讨论】:

    • 感谢您的回答,您有示例代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多