I have just partially understood (explain me like you would to a beginner) I know about transform, varibles, rigidbody and all that but I have not understood that xRotation, clamp and Quaternion.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
Debug.Log(mouseY);
Debug.log(mouseX);
}
}
It sounds like (maybe) English is not your first language so I will try to be clear. You need to ask a specific question here to get an answer. Forums are not tutorials. There are plenty of those online.
Go back to where you got the code from and here is how to do tutorials properly:
How to do tutorials properly:
Tutorials are a GREAT idea. Tutorials should be used this way:
Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.
If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.
Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.
Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.
Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
mouseY is the value of your mouse position change vertically on the screen.
It’s not the mouse Y position, it’s just how much the Y position has changed since last frame.
It will be a small positive or negative value.
Think of a first person camera. Typically when you pull the mouse back, you look down at the ground, when you push the mouse forwards, you look up at the sky.
In order to rotate the camera up and down, we actually have to rotate the camera on its X axis. You might have thought it would be the Y Axis, but you can test this out.
You can create a cube, and in its transform component there is the rotation x, y and z. Just drag the X value up and down, and you can see that that is the axis we want to use for looking up and down. Drag the Y value up and down, and see that it rotates horizontally, which isn’t what we want for the vertical look.
So, that’s why the variable is called XRotation, because we are rotating on the XAxis.
You could rename it to VerticalRotation if that is less confusing.
We are using XRotation to track our current vertical rotation, and we do that so that we can later clamp that value. mouse Y only contains the CHANGE in our vertical rotation, but it doesn’t track our actual rotation.
We SUBTRACT the mouseY from XRotation because if we don’t do that, the mouse look is inverted.
But see for yourself what that does.
Instead of:
xRotation -= mouseY;
change it to:
xRotation += mouseY;
and see how that inverts our vertical rotation
The clamping ensures that we can only look up and down so far (90 degrees up and 90 degrees down) before we can’t look further. That is to make sure we can’t flip the camera.
Once again, see for yourself.
comment out the line that clamps the rotation
//xRotation = Mathf.Clamp(xRotation, -90f, 90f);
and hit play and try to look up or down until your camera flips and the world is upside down and you’re looking behind your character.
So, because we are tracking our XRotation, we can tell whether it is greater than 90 or less than -90, and we can clamp it to those values
It’s very difficult to explain this without showing it, and without you experimenting to see why these things are done, so watch the mouselook section of this video and experiment:
As for Quaternion.Euler, that’s a function that takes Euler Angles and converts to a Quaternion, which is what localRotation takes. So we are converting it in order to set localRotation.
I don’t know anything about quaternions, most people just know how to convert between quaternions and euler angles, when required.
Note that in the video I posted, he doesn’t use localRotation and Quaternion.Euler
He applies the value directly using the LocalEulerAngle instead
So yeah, the only reason we need to track the XRotation, clamp the XRotation and assign the angle (unlike what we do with the player object horizontal rotation) is because we need to clamp the vertical rotation.