I want my script to be able to have my player (it’s a fps game) look around with the mouse. Sadly I don’t know how to make it look around and the script I came up with does not work:
public class MouseLock : MonoBehaviour {
void OnMouseEnter (){
}
void FixedUpdate ()
{
//Getting the value of Rotationx
Vector2 rotationx = new Vector2 (Input.mousePosition.y);
Vector2 rotationy = new Vector2 (Input.mousePosition.x);
transform.Rotate (rotationx, rotationy, 0.0f);
}
}
I have the errors on my screen but I have no idea how to solve these errors. Please help and explain how I am wrong. I am sorry if the error is very obvious but please help.
Best regards, Isaac.
PS If you could possibly tell me how to make the cursor spawn in the middle of the screen that would be great.
You should copy your eror to the question. But it has many errors anyway… Check some tutorials and understand example codes because it is alittle tricky at the begining.
“new Vector2 (Input.mousePosition.y);” a vector2 is a bidimensinal vector, meaning it has 2 values. It can not be created with just one value (input.mousePosition.y is one value).
You could create
Vector2 rotation = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
rotation.x will return the first value and rotation.y the second.
Then: transform.Rotate needs one single input: a Vector3 (made of three values).
You need to create the vector3 and then use it to rotate.
Your corrected script should look similar to this, but I’m not sure if your player will move as you want:
void FixedUpdate ()
{
//you create a v3 using 3 values
Vector3 rotation = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
//you use it to rotate
transform.Rotate (rotation);
}
Tutorils are very useful, keep learning.