Search Unity

Please explain me this code about moving camera along with the player

Discussion in 'Scripting' started by mangalamsrivastava2009, Aug 14, 2021.

  1. mangalamsrivastava2009

    mangalamsrivastava2009

    Joined:
    Oct 17, 2020
    Posts:
    38
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.  
    8.     public float mouseSensitivity = 100f;
    9.  
    10.     public Transform playerBody;
    11.  
    12.     float xRotation = 0f;
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    25.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    26.  
    27.         xRotation -= mouseY;
    28.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    29.  
    30.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    31.         playerBody.Rotate(Vector3.up * mouseX);
    32.  
    33.         Debug.Log(mouseY);
    34.         Debug.log(mouseX);
    35.     }
    36. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    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.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Obviously you did NOT write the above code.

    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!

    Here are some really good tutorial makers:

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:

     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    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.
     
    Last edited: Aug 14, 2021
    SausageMania likes this.
  5. mangalamsrivastava2009

    mangalamsrivastava2009

    Joined:
    Oct 17, 2020
    Posts:
    38
    I did write this code on my own and followed the same brackeys tutorial but he did not explain any code he just wrote them
     
  6. mangalamsrivastava2009

    mangalamsrivastava2009

    Joined:
    Oct 17, 2020
    Posts:
    38
    Thank you so much that can't be explained in words.