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

Player moving in the direction of the camera

Discussion in 'Scripting' started by GaminBever, Feb 16, 2021.

  1. GaminBever

    GaminBever

    Joined:
    Nov 27, 2020
    Posts:
    2
    Hi there!

    I am very new to unity and I'm trying to create a third person game. my character can move normally but I don't know how to make it so you move in the direction that the camera is facing.

    This is my character controller:

    Code (CSharp):
    1.  
    2. public class ThirdPersonMovement2 : MonoBehaviour
    3. {
    4.     public Rigidbody rb;
    5.  
    6.     public float ForwardForce = 100f;
    7.     public float SidewaysForce = 100f;
    8.     public float BackwardsForce = 100f;
    9.  
    10.     void Update()
    11.     {
    12.  
    13.         if (Input.GetKey("w"))
    14.         {
    15.             rb.AddForce (0, 0, ForwardForce);
    16.         }
    17.  
    18.         if (Input.GetKey("a"))
    19.         {
    20.             rb.AddForce (-SidewaysForce, 0, 0);
    21.         }
    22.  
    23.         if (Input.GetKey("d"))
    24.         {
    25.             rb.AddForce (SidewaysForce, 0, 0);
    26.         }
    27.  
    28.         if (Input.GetKey("s"))
    29.         {
    30.             rb.AddForce (0, 0, -ForwardForce);
    31.         }
    32.  
    33.     }
    34. }
    35.  
    the player itself is a parent object that has a rigidbody, a capsule collider and the script, with a child object that has all of the graphics of the player.

    please help me because I have no idea how to make the character move in the direction of the camera and I would really like to make this work.
     
  2. ZioBasi

    ZioBasi

    Joined:
    May 8, 2018
    Posts:
    33
    You mean the camera that follow the player and not the vice-versa right?
    If that's right this is a good tutorial where you can also make cool stuff while the player is aiming:


    But if you need something for beginner you can find lot of tutorials into Unity Learn website

    Ps. Have you ever tried the new Input System to create player's input instead of writing the code manually? It's really good to start a prototype or to learn and if you need to implement something with the code you can always do it, just to keep everything cleaner. Check it out ;)
     
    Last edited: Feb 16, 2021
  3. GaminBever

    GaminBever

    Joined:
    Nov 27, 2020
    Posts:
    2
    no, my camera is good but whenever i change where to camera is facing the player still moves in the same direction. i want the player to move in the direction that the camera is facing but i don't know how to do that.