Click to Move rotation problems

Working on a Click to Move script that moves the player object towards the mouse position while the mouse button is held down. My issue right now is that whenever the object collides with something, it’ll just start rotating slowly in a circle, even after the mouse button is released. I even added a line after the “while” loop to reset the transform’s rotation if the button is released, but this doesn’t seem to have any effect at all. Hope someone can help!

I’ve added a quick video illustrating my issue:

var moveSpeed : int;

function Update () 
{
	if (Input.GetMouseButtonDown(1))
	{
		Move ();
	}
}


function Move () 
{
	while (Input.GetMouseButton(1))
	{
		var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
		var mask : int = (1 << 8);
		var walkDistance : float = 4.0;
		if(Physics.Raycast (ray, hit, Mathf.Infinity, mask))
		{
			//Rotate the body
			var targetRotation = Quaternion.LookRotation(hit.point - transform.position);
			var targetRotationY : float = targetRotation.eulerAngles.y;
			var facingDirection : float = Mathf.LerpAngle(transform.eulerAngles.y, targetRotationY, Time.deltaTime * 3);
			transform.eulerAngles = Vector3(0, facingDirection, 0);
			
			//Check distance from the body towards the mouse position
			Debug.DrawRay(transform.position, hit.point, Color.red);
			var targetDistance = Vector3.Distance(transform.position, hit.point);
			if(targetDistance > 1.2)
			{
				if(targetDistance > walkDistance)
				{
					moveSpeed = 6;
				} else {
					moveSpeed = 2;
				}
				//Move the body
				transform.position += (hit.point - transform.position).normalized * moveSpeed * Time.deltaTime;		
			}
		}
		yield;
	}
	transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
}

Do you have a rigidbody applied to this gameobject? If so mark it as kinematic or freeze rotation.

You might want to only change the X,Y,Z values in your code.

I tried using a rigidbody and freeze some of the rotations, but it doesnt work well. You get a much better result by restricting it in the code, by only changing the rotations that you want to use. Alternatively I think you could use MathF.Clamp() or something.

Hm, after some more test it does seem it’s the rigidbody that’s breaking the script. When I remove every line instance that alters the game object’s rotation, it still orbits after colliding with any object. I thought by coding the y rotation, I could freeze the transform after collisions, but this doesn’t seem to be having any effect.

Marking it kinematic makes it fall through the floor, or just pass through other colliders. Is there a work-around for this, because the object still needs to be affect colliders?

As Roi and I stated, you could freeze the rotationaxis of your rigidbody.
Also I read somewhere that if you move a kinematic rigidbody by transform its best to do that from FixedUpdate instead of Update.

And through code you could set
rigidbody.angularVelocity = new Vector3(0,0,0);

Thanks a bunch, I’ll experiment with this!