效果(用鼠标控制副物体旋转):
通公式获取圆的下一个坐标位置:
x = vector3.x + r * cos(angle * PI / 180)
z = Vector3.z + r * sin(angle * PI /180)
源代码:
public class Test : MonoBehaviour
{
private Transform CylinderTranform;
private float r;
// Use this for initialization
void Start()
{
CylinderTranform = GameObject.Find("Cylinder").transform;//获取主物体坐标
r = Vector3.Distance(transform.position, CylinderTranform.position);//获取主物体与跟随物体距离,即半径
}
float MouseX;
// Update is called once per frame
void Update()
{
transform.LookAt(CylinderTranform);
MouseX += Input.GetAxis("Mouse X")*5;//获取鼠标位置*5
//主物体X+半径*cos(鼠标位置*pi/180)
var x = CylinderTranform.position.x + r*Mathf.Cos(-MouseX*Mathf.PI/180);
var z = CylinderTranform.position.z + r*Mathf.Sin(-MouseX*Mathf.PI/180);
transform.position = new Vector3(x, CylinderTranform.position.y, z);
}
}