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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Player rotate based on camera, tilting down

Discussion in 'Scripting' started by Darkadin, Nov 22, 2022.

  1. Darkadin

    Darkadin

    Joined:
    Oct 28, 2022
    Posts:
    8
    My movement is relative to my camera. Everything works almost perfectly. I can move in all directions, my player faces the relative direction based on the camera (cinemachine), my problem is the player also tilts forward and backward. I think its because the camera is sometimes tilted down looking at the player because when I adjust it just at the right angle, I can take away that tilt. After digging, I did have some code that I think worked, but I had rewrote my code so many times I just ended up getting rid of this one line that I think I need. Something to do with euler.angles? I cant figure it out.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor.Experimental.GraphView;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class PlayerMove : MonoBehaviour
    9. {
    10.     //Variables
    11.     private Rigidbody rb; //Sets rigidbody
    12.     [SerializeField] float moveSpeed = 1f; //How fast
    13.     [SerializeField] float jumpPower = 1f; //How high jump
    14.     [SerializeField] GameObject cam; //Grabs from cinemachine camera
    15.  
    16.  
    17.     private void FixedUpdate()
    18.     {
    19.         Movement();
    20.     }
    21.  
    22.     void Movement()
    23.     {
    24.         //Variables
    25.         float xValue = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; //Horizontal axis
    26.         float zValue = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed; //Vertical Axis
    27.         float yValue = Input.GetAxis("Jump") * Time.deltaTime * jumpPower; //Jumps
    28.         Vector3 playerMovement = new Vector3(xValue, 0, zValue); //Puts all axis into Vector3
    29.         rb = GetComponent<Rigidbody>(); //Grabs rigidbody
    30.  
    31.         //Movement and Direction
    32.         Vector3 moveVector = cam.transform.TransformDirection(playerMovement) *moveSpeed; //Sets Vector3 in relation to camera
    33.         rb.velocity = new Vector3(moveVector.x, rb.velocity.y, moveVector.z); //Moves using velocity
    34.  
    35.         if (playerMovement != Vector3.zero) //If player is  moving
    36.         {
    37.             transform.forward = moveVector; //Sets rotation to movement direction
    38.         }
    39.  
    40.         //Jump
    41.         if (Input.GetButtonDown("Jump"))
    42.         {
    43.             rb.AddForce(0, jumpPower, 0, ForceMode.Impulse);
    44.         }
    45.     }
    46. }
     
  2. Darkadin

    Darkadin

    Joined:
    Oct 28, 2022
    Posts:
    8
    I don't know exactly how this forum board works but I'm assuming this hasn't actually posted publicly yet so really this message I guess is just for mods but I figured out my code by adding


    Code (CSharp):
    1.             transform.localEulerAngles = new Vector3(10, transform.localEulerAngles.y, 0); //Makes sure only y rotates I think?
    2.  
    to line 38
     
  3. Darkadin

    Darkadin

    Joined:
    Oct 28, 2022
    Posts:
    8
    PS: In case anyone years later magically finds this post and it helps them out, I added the 10 to the x value in my added code because I actually liked a small tilt forward while moving ironically, anyway, that's why its 10 and not 0.