Unity2D:让多个对象围绕SAME中心旋转,但从偏移位置开始

问题描述 投票:0回答:2

我希望我游戏中的三个浮动2D平台围绕相同的中心旋转。我这样做了:

public GameObject Object;

public float RotateSpeed;
public float Radius;

private Vector2 _centre;
private float _angle;

private void Start()
{
    _centre = transform.position;
}

private void Update()
{

    _angle += RotateSpeed * Time.deltaTime;

    var offset = new Vector2(Mathf.Sin(_angle), Mathf.Cos(_angle)) * Radius;
    Object.transform.position = _centre + offset;
}

它工作得很好,但是 - 只适用于一个平台。如果我在混合中添加更多平台,它们要么全部从围绕中心的假想圆内的相同位置开始,如果我偏移它们,它们都会以不同的中心旋转(如果偏移是x = 2,那么中心也被2)抵消。我怎么说3个平台围绕同一个中心圈,但是从圈子上的3个不同的起点开始? (如0°,180°,270°)左右? (目前,所有3个从0开始)

谢谢

编辑:

这就是我想要旋转的方式。 4个矩形是平台。它们都围绕想象中心旋转。它们都是相同的速度所以永远不会彼此靠近。而且,他们都保持直立。蓝线和红线也是虚构的。中心没有物体。希望这可以帮助

enter image description here

c# unity3d
2个回答
1
投票

将变量:private float _angle;定义为public:public float _angle;

在此之后,通过单击附加脚本的对象转到检查器,并查找名为“angle”的变量。将此变量更改为每个平台的不同角度。


0
投票

这是因为Vector2 _centre被实例化为对象的初始位置,尝试获取枢轴的变换而不是获取对象本身的变换

public float RotateSpeed;
public float Radius;

public Transform _centre; // in the inspector input the pivot transform
public float _angle; // set it in the inspector
// private float _angle; use this with the code in the start method

private void Start()
{
    /*
    _angle = Mathf.asin((pivot.position.x - transform.position.x)/Radius);
    */
}

private void Update()
{

    _angle += RotateSpeed * Time.deltaTime;

    var offset = new Vector2(Mathf.Sin(_angle) * Radius, Mathf.Cos(_angle)) * Radius;
    transform.position = _centre + offset;
}

确保将平台放置在旋转枢轴周围的连贯位置。

© www.soinside.com 2019 - 2024. All rights reserved.