Unity一个物体随另一个物体旋转,圆形和椭圆形

本文介绍了一个Unity3D中的脚本实现,通过该脚本可以使一个游戏对象围绕另一个目标对象进行椭圆形路径环绕运动。该脚本利用了Unity3D的Transform组件和数学函数来实现动态位置更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

懒得写了,直接上代码

public class Rotate : MonoBehaviour
{
public GameObject target;
public GameObject m_this;
public float speed = 100;
float progress = 0;
float distance = 0;
public float x = 5;
public float z = 7;
private void Awake()
{
distance = Vector3.Distance(target.transform.position, m_this.transform.position);
}
private void Update()
{
OvalRotate();
}
//绕圆形旋转
void RoundnessRotate()
{
progress += Time.deltaTime*speed;
Vector3 p = new Vector3(Mathf.Cos(progress * Mathf.Deg2Rad), 0, Mathf.Sin(progress * Mathf.Deg2Rad)) * distance;
m_this.transform.position = target.transform.position + p;
}
//绕椭圆形旋转
void OvalRotate()
{
progress += Time.deltaTime * speed;
Vector3 p = new Vector3(x * Mathf.Cos(progress * Mathf.Deg2Rad), 0,z* Mathf.Sin(progress * Mathf.Deg2Rad)) * distance;
m_this.transform.position = target.transform.position + p;
}
}

转载于:https://www.cnblogs.com/zhanghebk/p/7531865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值