Rotate to Mouse Position is not working well.(VIDEO)

Hi guys,

(LONG STORY SHORT you can skip to Video Record (Second Test Unity Rotate problem Shaking Vibrating - YouTube) )

i am writing a Diablo-like game with orbit camera. Character must look(rotate) to mouse position. I am using ScreenPointToRay and it looks work correct but when i look hit.points which is hit to terrrain points, y must be 0 but it is not 0. it can be more than 0 sometimes(35, 48, like randomly generated numbers). So it makes my character does not Rotate correctly. It is very weird rotation. I recorded a video. First of all here is my code;

void Update () {
    mouseToWorld ();
    moveToPosition ();

Vector3 mouseToWorld(){

	RaycastHit hit;
	mRay = cam.ScreenPointToRay(Input.mousePosition);
	if (debug_CamToWorldRay==true)
		Debug.DrawRay (mRay.origin, mRay.direction*999, Color.yellow);
	if (Physics.Raycast (mRay.origin, mRay.	direction, out hit, 9999)) {
			mousePosition = hit.point; }
		return new Vector3 (hit.point.x,0,hit.point.z);
		}else return;

void moveToPosition(){

	Quaternion newRotation = Quaternion.LookRotation (mouseToWorld() - transform.position, Vector3.forward);
	newRotation.x = 0f;
	newRotation.z = 0f;
	transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * rotationSpeed);
	if (Input.GetKey (KeyCode.Mouse1))
					cc.SimpleMove (transform.forward * runSpeed);

There is nothing wrong with moving part. If it is looking somewhere, it can move through there. But rotating like that ,here is my video of Scene view , also game view is same of course, But i recorded scene so you can see the Ray.

Your issue is that you are directly manipulating the components of a Quaternion. They cannot be handled like Euler Angles. Here is an alternate solution for your rotation:

Vector3 dir = mouseToWorld() - transform.position;
dir.y = 0.0;
Quaternion newRotation = Quaternion.LookRotation (dir);
transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * rotationSpeed);

I’m not sure why you had ‘Vector3.forward’ as the last parameter in your LookRotation. I believe you want to rotate around Vector3.up (the default). If your terrain becomes hilly, you may want to use transform.up instead to preferentially rotate around the character’s ‘y’.

A couple of other notes. First (minor), there is a version of Physics.Raycast() that take a ray instead of a position and direction. You don’t have to split up the ray when making the call. Second, there is a Collider.Raycast() that you could use to Raycast() against just the terrain (and therefore your Raycast() would ignore all other objects).

I found the problem. It was my line of sight collider of player. Ray hits this collider and gives bad position points. Sorry for this ignorant thing. I was not know until i debug hit.collider.tag . Thanks for everything.