Hello all. I’ve run into a weird issue when moving the character controller in local space. I’m rotating an object around a sphere using joystick inputs. The object is the child of the sphere, and movement is happening in local space. The issue I run into is that the radius on which the object rotates slowly increases as it moves. Can’t for the life of me figure out why. So, for example, if the object starts touching the outside of the sphere, after several rotations it’s moved away from the surface. Any recommendations on how to limit this would be appreciated.
public class Movement : MonoBehaviour
{
public float speed = 4;
Vector3 movement;
Vector3 up;
public Transform planet;
CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
//Movement
movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized * speed;
//Translate movement from world to local space
movement = transform.TransformDirection(movement);
//Move
controller.Move(movement);
//Rotate so that up is always away from center
up = transform.position - planet.position;
transform.rotation = Quaternion.FromToRotation(transform.up, up) * transform.rotation;
}
}