Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make Player rotate relative to the Camera

Discussion in 'Scripting' started by Ben_Jah-Mun, Nov 9, 2021.

  1. Ben_Jah-Mun

    Ben_Jah-Mun

    Joined:
    Jun 25, 2020
    Posts:
    5
    I'm trying to make my player rotate relative to the camera, it moves properly but it just doesn't rotate properly.

    This is the Script:

    Code (CSharp):
    1. void Update()
    2.    {
    3.        // get the horizontal and vertical inputs
    4.        float horizontal = Input.GetAxis("Horizontal") * maxSpeed;
    5.        float vertical = Input.GetAxis("Vertical") * maxSpeed;
    6.      
    7.        Vector3 camF = cam.forward;
    8.        Vector3 camR = cam.right;
    9.        camF.y = 0;
    10.        camR.y = 0;
    11.        camF = camF.normalized;
    12.        camR = camR.normalized;
    13.      
    14.        transform.position += (camF*vertical + camR*horizontal)*Time.deltaTime*5;
    15.      
    16.        // set our velocity based on our inputs
    17.        rig.velocity = new Vector3(horizontal, rig.velocity.y, vertical);
    18.        rig.velocity = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    19.      
    20.        // create a copy of our velocity variable and
    21.        // set the y axis to be 0
    22.        Vector3 vel = rig.velocity;
    23.        vel.y = 0;
    24.      
    25.        // if we're moving, rotate to face our moving direction
    26.        if(vel.x != 0 || vel.z != 0)
    27.        {
    28.            transform.forward = vel;      
    29.        }
     
  2. ubbelito

    ubbelito

    Joined:
    Sep 24, 2018
    Posts:
    23
    Something like this? It seems that you are already rotating the camera, as you said that the camera is properly aligned. So I just copied the camera rotation. As an alternative, I added code to make the player face the velocity. You can pick either of these.

    Code (CSharp):
    1. void Update()
    2. {
    3.     // get the horizontal and vertical inputs
    4.     float horizontal = Input.GetAxis("Horizontal") * maxSpeed;
    5.     float vertical = Input.GetAxis("Vertical") * maxSpeed;
    6.  
    7.     Vector3 velocity = (vertical * cam.forward + horizontal * cam.right) * 5;
    8.  
    9.     // alt 1
    10.     transform.rotation = cam.rotation;
    11.  
    12.     // alt 2
    13.     transform.LookAt(transform.position + velocity);
    14.  
    15.     // Apply the change in position based on velocity.
    16.     transform.position += velocity * Time.deltaTime;
    17. }
    18.  
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
  4. Ben_Jah-Mun

    Ben_Jah-Mun

    Joined:
    Jun 25, 2020
    Posts:
    5
  5. Ben_Jah-Mun

    Ben_Jah-Mun

    Joined:
    Jun 25, 2020
    Posts:
    5
    The transform.rotation one works but he can't go down anymore, only vertical and horizontal.
     
  6. Ben_Jah-Mun

    Ben_Jah-Mun

    Joined:
    Jun 25, 2020
    Posts:
    5
    Actually, Im not sure if that's from your script or if its just like that. For some reason he falls really slowly. I mustve done something wrong. It was working fine last time I used it!?
     
  7. Ben_Jah-Mun

    Ben_Jah-Mun

    Joined:
    Jun 25, 2020
    Posts:
    5
    Actually, the tranform.LookAt one seems to work fine. Thanks.
     
    ubbelito likes this.