我有一个轨道脚本来控制围绕行星运行的gameObject。它的轨道运转良好(很少起伏。)但是如何控制其起始位置和轨道呢?例如,具有特定起始位置的随机轨道(反之亦然),随机轨道和随机起始点等。
我只想通过了解向量就能控制轨道。
public GameObject orbitAroundTarget;
private Transform centerOfOrbit;
public Vector3 orbitAxis = Vector3.up;
public Vector3 desiredOrbitPosition;
public float orbitRadius = 100f;
public float orbitAdjustSpeed = 10f;
public float orbitSpeed = 2f;
void OnEnable()
{
centerOfOrbit = orbitAroundTarget.transform;
transform.position = (transform.position - centerOfOrbit.position).normalized * orbitRadius + centerOfOrbit.position;
}
void FixedUpdate()
{
transform.RotateAround(centerOfOrbit.position, orbitAxis, orbitSpeed * Time.deltaTime);
desiredOrbitPosition = (transform.position - centerOfOrbit.position).normalized * orbitRadius + centerOfOrbit.position;
transform.position = Vector3.MoveTowards(transform.position, desiredOrbitPosition, Time.deltaTime * orbitAdjustSpeed);
}