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)) {
if(hit.transform.name=="Terrain"){
mousePosition = hit.point; }
Debug.Log(hit.point);
return new Vector3 (hit.point.x,0,hit.point.z);
}else return Vector3.zero;
}
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.