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 How do I make the player move in the same direction that the camera is facing in unity

Discussion in 'Scripting' started by ansonisawesome24, May 30, 2020.

  1. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    This is my movement script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.  
    9.     bool isGrounded = true;
    10.     public float distToGround = 1f;
    11.  
    12.     public float forwardForce = 500f;
    13.     public float sidewaysForce = 500f;
    14.     public float jumpForce = 500f;
    15.     public float distance = 5;
    16.  
    17.     void FixedUpdate()
    18.     {
    19.         var look = new Vector3(Camera.main.transform.forward.x, transform.position.y, Camera.main.transform.forward.z);
    20.  
    21.         if (Input.GetKey("w"))
    22.         {
    23.             rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    24.         }
    25.  
    26.         if (Input.GetKey("s"))
    27.         {
    28.             rb.AddForce(0, 0, -forwardForce * Time.deltaTime);
    29.         }
    30.  
    31.         if (Input.GetKey("d"))
    32.         {
    33.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
    34.         }
    35.  
    36.         if (Input.GetKey("a"))
    37.         {
    38.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
    39.         }
    40.  
    41.         if (Input.GetKey("space") && isGrounded)
    42.         {
    43.             rb.AddForce(0, jumpForce * Time.deltaTime, 0);
    44.         }
    45.  
    46.         GroundCheck();
    47.     }
    48.  
    49.     void GroundCheck()
    50.     {
    51.         if (Physics.Raycast(transform.position, Vector3.down, distToGround + 0.1f))
    52.         {
    53.             isGrounded = true;
    54.         }
    55.         else
    56.         {
    57.             isGrounded = false;
    58.         }
    59.     }
    60. }
    This is my camera look script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LookAround : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.  
    9.     public float mouseSensitivity = 100.0f;
    10.     public float clampAngle = 80.0f;
    11.  
    12.     private float rotationY = 0.0f;
    13.     private float rotationX = 0.0f;
    14.  
    15.     private float distance = 0.0f;
    16.  
    17.     void Start()
    18.     {
    19.         Vector3 rotation = transform.localRotation.eulerAngles;
    20.         rotationY = rotation.y;
    21.         rotationX = rotation.x;
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         float mouseX = Input.GetAxis("Mouse X");
    27.         float mouseY = -Input.GetAxis("Mouse Y");
    28.  
    29.         rotationY += mouseX * mouseSensitivity * Time.deltaTime;
    30.         rotationX += mouseY * mouseSensitivity * Time.deltaTime;
    31.  
    32.         rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
    33.  
    34.         Quaternion localRotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
    35.         transform.rotation = localRotation;
    36.     }
    37. }
     
  2. elliotfriesen

    elliotfriesen

    Joined:
    Mar 17, 2020
    Posts:
    71
    do this instead,
    private GameObject cam;
    public float speed;
    void Start()
    {
    cam = Camera.main;
    }
    then in your movement put this
    rb.AddForce(cam.transform.forward * Speed * Time.deltaTime);
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
  4. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    on the line cam = Camera.main; I get an error saying cannot implicitly convert type 'UnityEngine.Camera' to UnityEngine.GameObject'.I tried deleting the line and replacing the cam in the line rb.AddForce(cam.transform.forward * Speed * Time.deltaTime); with Camera.main, but it still didn't work.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    I think it's just a typo in Elliot's code. This is probably what he meant:

    Code (csharp):
    1. cam = Camera.main.gameObject;
    The reason is,
    Camera.main
    is a shortcut to the camera tagged with "Main Camera", and he was trying to assign the
    Camera
    component to a
    GameObject
    variable (in this case
    cam
    ). Easy mistake.

    Meanwhile, the proximity_buttons demo I liked above does all the things out-of-box, so you're welcome to take a look at it.
     
  6. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    ok thanks, I want to learn it by experience instead of just copying. I checked out your code and it was great.

    and also it still didn't work.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    My code or the other snippet I responded to? I haven't studied how Elliot's trying to do it, as like I said I just do it "my way." :)

    Let me know if my code is misbehaving.
     
  8. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    Elliots code
     
  9. elliotfriesen

    elliotfriesen

    Joined:
    Mar 17, 2020
    Posts:
    71
    I didn't write the code in the editor but thats what I meant, also for me the code i put, it would usually work so its strange that it doesnt work for you
     
  10. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    yeah, I don't know either, but do you know any other way?
     
  11. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    Yeah, i figured out another way that hasn't been done before i think. I added another camera that only rotated on the x-axis, and used that camera instead of the main camera for this line of code transform.position = transform.position + xCam.transform.forward * distance * Time.deltaTime;
    this is what i used to make the camera rotate
    Code (CSharp):
    1. public class Cam2FollowCam1 : MonoBehaviour
    2. {
    3.     public GameObject player;
    4.  
    5.     public float mouseSensitivity = 100.0f;
    6.     public float clampAngle = 80.0f;
    7.  
    8.     private float rotationY = 0.0f;
    9.     private float rotationX = 0.0f;
    10.  
    11.     private float distance = 0.0f;
    12.  
    13.     void Start()
    14.     {
    15.         //Remember to make a look script that only looks left and right for a new camera object.
    16.  
    17.         Vector3 rotation = transform.localRotation.eulerAngles;
    18.         rotationY = rotation.y;
    19.         rotationX = rotation.x;
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         float mouseX = Input.GetAxis("Mouse X");
    25.         float mouseY = -Input.GetAxis("Mouse Y");
    26.  
    27.         rotationY += mouseX * mouseSensitivity * Time.deltaTime;
    28.         rotationX += mouseY * mouseSensitivity * Time.deltaTime;
    29.  
    30.         rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
    31.  
    32.         //Quaternion localRotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
    33.         Quaternion localRotation = Quaternion.Euler(0.0f, rotationY, 0.0f);
    34.         transform.rotation = localRotation;
    35.     }
    36. }