- Home /
Rotation offset help!
Hi first off i apologize for all the very similar posts recently.. it wouldn't let me delete them after someone had commented :(.. but i have figured out a "mouse look" script.. and it works great.. but there is a slight offset in the way the character looks.. like he is rotated to far to the right.. is there a way i could ad a rotation offset so he would look directly where the mouse is? heres the code:
using UnityEngine; using System.Collections;
public class LookAtMouse : MonoBehaviour {
private Vector3 inputRotation;
private Vector3 mousePlacement;
private Vector3 screenCenter;
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
Look();
transform.rotation = Quaternion.LookRotation (inputRotation);
}
}
void Look ()
{
screenCenter = new Vector3 (Screen.width * 0.5f, 0, Screen.height * 0.5f);
mousePlacement = Input.mousePosition;
mousePlacement.z = mousePlacement.y;
mousePlacement.y = 0;
inputRotation = mousePlacement - screenCenter;
}
}
private Vector3 inputRotation;
private Vector3 mousePlacement;
private Vector3 screenCenter;
void Update ()
{
if (Input.Get$$anonymous$$ouseButtonDown (0))
{
transform.rotation = Quaternion.LookRotation (Look());
}
}
private Vector3 Look ()
{
screenCenter = new Vector3 (Screen.width * 0.5f, Screen.height * 0.5f, 0);
mousePlacement = Input.mousePosition;
mousePlacement.z = mousePlacement.y;
mousePlacement.y = 0;
inputRotation = mousePlacement - screenCenter;
return inputRotation
}
I would just fix that up a little bit then try playing with the width and height.
Oh sorry, it was Vector3 not float return type
private Vector3 Look () {
Thanks that made it work better!! any idea what i should do with the screen height and width?? im messing around with it.. but i can't seem to get it perfect
Just looking it over I am not too familiar with screen height but I think that you should switch the Z to the Y
screenCenter = new Vector3 (Screen.width * 0.5f, Screen.height * 0.5f, 0);
The positioning in vector 3 goes 'X Y Z' x is left and right. Y is up and down . z is forward and backwards.