Hi all, I have been struggling with trying to get to control a character through a character controller, but only in circular motion. I have stumbled upon the following script and it works! But not quite… I am observing some weird drift away from the center of rotation. I have verified this by checking the actual distance:
var distance = Vector3.Distance(target.position, transform.position);
Debug.Log (distance);
It gets even stranger, because the direction in which the object is moving does not matter (i.e. CW and CCW both increase the distance between the object and its target).
Because I did not fully understand exactly how the maths behind this worked, I tried writing something similar on my own based on LookAt:
var speed : float = 60.0;
var target : Transform;
private var moveDirection : Vector3 = Vector3.zero;
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.LookAt(target.position);
var right = transform.right;
right *= Input.GetAxis("Horizontal");
Debug.DrawRay (transform.position, right*5f, Color.red);
moveDirection = right;
moveDirection *= speed;
controller.Move(moveDirection * Time.fixedDeltaTime);
var distance = Vector3.Distance(target.position, transform.position);
Debug.Log (distance);
}
I have also removed gravity, so the character controller basically orbits the object around its target, but I still get this increase in distance between the two objects.
I am wondering if this is something to do with the way CharacterController.Move() works, or Character Controllers in general… I have also noticed that this drift is related to the speed variable - this makes me think it might be due to float imprecision.
Does anyone have a clue why this might be happening? Any solutions to the problem? If not, what would be a good way of implementing circular motion within a character controller?