我正在为我的 2d 游戏添加一架直升机,我需要它在 x 轴上移动的同时做圆周运动。您可以在下面找到我正在使用的使用数学圆方程的代码。
angle += speed * Time.deltaTime; //if you want to switch direction, use -= instead of +=
float x = startPoint.x + Mathf.Cos(angle) * radius;
float y = startPoint.y + Mathf.Sin(angle) * radius;
transform.position = new Vector2(x + 2, y);
1)制作一个空的游戏对象
2)将您的盒子作为空游戏对象的父级
3)围绕空的游戏对象旋转盒子
4)将空的游戏对象移到一边
如果你想避免添加一个空的父级,你可以单独跟踪旋转中心,围绕它旋转,并随着时间的推移移动它。
public class hello_rotate : MonoBehaviour
{
float angle = 0;
float radius = 1;
float speed = 10;
float linear_speed = 1;
Vector2 centerOfRotation;
// Start is called before the first frame update
void Start()
{
centerOfRotation = transform.position;
}
// Update is called once per frame
void Update()
{
centerOfRotation.x = centerOfRotation.x + linear_speed * Time.deltaTime;
angle += speed * Time.deltaTime; //if you want to switch direction, use -= instead of +=
float x = centerOfRotation.x + Mathf.Cos(angle) * radius;
float y = centerOfRotation.y + Mathf.Sin(angle) * radius;
transform.position = new Vector2(x + 2, y);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句