UNITY 围绕一个物体做圆周运动

有一道题:一个物体围绕原点(0,0)做匀速圆周运动。t1时物体位置为(x1,y1),已知物体的角速度为 w,围绕半径为 r。求经过时间t后,物体在圆周上位置(x,y)。

求得:
- x = r * sin(w * t)
- y = r * cos(w * t)


using UnityEngine;

public class RotateAround : MonoBehaviour {
    public Transform aroundPoint;//围绕的物体
    public float angularSpeed;//角速度
    public float aroundRadius;//半径

    private float angled;
    void Start () {
        //设置物体初始位置为围绕物体的正前方距离为半径的点
        Vector3 p = aroundPoint.rotation * Vector3.forward * aroundRadius;
        transform.position = new Vector3(p.x, aroundPoint.position.y, p.z);
    }

    void Update () {
        angled += (angularSpeed * Time.deltaTime) % 360;//累加已经转过的角度
        float posX = aroundRadius * Mathf.Sin(angled * Mathf.Deg2Rad);//计算x位置
        float posZ = aroundRadius * Mathf.Cos(angled * Mathf.Deg2Rad);//计算y位置

        transform.position = new Vector3(posX, 0, posZ) + aroundPoint.position;//更新位置
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值