我们来写一个例子,让Cube物体在距离Sphere 2米处绕Sphere旋转
代码:
public class t_Distance : MonoBehaviour {
public Transform target;
//旋转距离
public float targetDistance = 2.0f;
//是否到达旋转的距离
private bool arriveDistance = false;
void Update () {
if (arriveDistance) {
transform.RotateAround(target.position, Vector3.up, Time.deltaTime * 20);
transform.LookAt(target);
}
else {
float distance = Vector3.Distance(transform.position, target.position);
if (distance <= targetDistance)
{
arriveDistance = true;
}
else {
transform.position = Vector3.Lerp(transform.position,target.position,Time.deltaTime);
}
}
}
}
效果: