I’m trying to make a ship sail around a sphere but i’m having some issues turning it when i need too. The code i have makes it sail perfectly in a straight line around the sphere but the moment i need it to turn it flips out.
If i comment out the transform.rotation in the Update() and use RotateTowards(). It will turn towards the target but it wont align the ship with the surface. If i leave that line uncommented anything RotateTowards does is ignored.
void Update()
{
GetFSM().Update();
lastPosition = transform.position;
Vector3 newPos = lastPosition + transform.rotation * ForwardSpeed * Time.fixedDeltaTime * 0.1f;//
newPos = newPos.normalized * altitude;
transform.position = newPos;
transform.rotation = Quaternion.FromToRotation( Vector3.up, newPos ) * Quaternion.Euler(0, angle, 0);*/
}
public void RotateTowards (Vector3 targetPos)
{
Vector3 relative = transform.InverseTransformPoint( targetPos );
angle = (Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg) * Time.deltaTime ;
transform.Rotate (0, angle * Time.deltaTime, 0);
}