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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

*HELP ME* How can I make the player movement direction change with the camera horizontal movement?

Discussion in 'Scripting' started by pimentalamego, Apr 9, 2018.

  1. pimentalamego

    pimentalamego

    Joined:
    Apr 9, 2018
    Posts:
    3
    I'm making a rolling ball game and I got the camera to rotate with the player but the movent (w,a,s,d) does not follow the camera transformation which leaves the controler a mess. Like the camera rotate around my player like I want to, but the orientation of the player keeps the same. How can i change that the player movement change with the camera?

    Code (CSharp):
    1. {
    2.     public float speed;
    3.     public Text countText;
    4.     public Text winText;
    5.  
    6.     private Rigidbody rb;
    7.     private int count;
    8.  
    9.     void Start()
    10.     {
    11.         rb = GetComponent<Rigidbody>();
    12.         count = 0;
    13.         SetCountText();
    14.         winText.text = "";
    15.     }
    16.     void FixedUpdate()
    17.     {
    18.         float moveHorizontal = Input.GetAxis("Horizontal");
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.  
    21.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    22.  
    23.         rb.AddForce(movement * speed);
    24.  
    25.         Vector3 direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
    26.  
    27.         direction = Camera.main.transform.TransformDirection(direction);
    28.         direction.y = 0.0f;
    29.  
    30.  
    31.  
    32.     }
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    the code has a few things wrong with it.

    here are my edits with notes

    Code (CSharp):
    1. {
    2.     public float speed;
    3.     public Text countText;
    4.     public Text winText;
    5.     private Rigidbody rb;
    6.     private int count;
    7.     private Camera mainCam; // or make this public and drag the camera to this element
    8.     public Vextor3 offset;
    9.     void Start()
    10.     {
    11.         mainCam = GameObject.Find("name of camera gameObject").GetComponent<Camera>(); // change the cam name  //not needed if you set it to public
    12.         rb = GetComponent<Rigidbody>();
    13.         count = 0;
    14.         SetCountText();
    15.         winText.text = "";
    16.     }
    17.  
    18.  
    19.     void FixedUpdate()
    20.     {
    21.         float moveHorizontal = Input.GetAxis("Horizontal");
    22.         float moveVertical = Input.GetAxis("Vertical");
    23.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    24.         rb.AddForce(movement * speed);
    25.         Vector3 direction = new Vector3(moveHorizontal, 0f, moveVertical); // this is not needed, same as movement,  and you override this in the next line
    26.         direction = Camera.main.transform.TransformDirection(direction); //resets the direction values
    27.         direction.y = 0.0f; //this does nothing
    28.       //direction does nothing from what i can see
    29.         mainCam.transform.position = transform.position + offset;
    30.     }
     
  3. pimentalamego

    pimentalamego

    Joined:
    Apr 9, 2018
    Posts:
    3
    So I changed the code as you gave me, (you can see below what I changed) and now is giving me an error saying that the name `SetCountText` does not exist in the current context.
    Code (CSharp):
    1. public float speed;
    2.     public Text countText;
    3.     public Text winText;
    4.     private Rigidbody rb;
    5.     private int count;
    6.     private Camera mainCam; // or make this public and drag the camera to this element
    7.     public Vector3 offset;
    8.     void Start()
    9.     {
    10.         mainCam = GameObject.Find("mainCam").GetComponent<Camera>(); // change the cam name  //not needed if you set it to public
    11.         rb = GetComponent<Rigidbody>();
    12.         count = 0;
    13.         SetCountText();
    14.         winText.text = "";
    15.     }
    16.  
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         float moveHorizontal = Input.GetAxis("Horizontal");
    21.         float moveVertical = Input.GetAxis("Vertical");
    22.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    23.         rb.AddForce(movement * speed);
    24.         Vector3 direction = new Vector3(moveHorizontal, 0f, moveVertical); // this is not needed, same as movement,  and you override this in the next line
    25.         direction = Camera.main.transform.TransformDirection(direction); //resets the direction values
    26.         direction.y = 0.0f; //this does nothing
    27.                             //direction does nothing from what i can see
    28.         mainCam.transform.position = transform.position + offset;
    29.     }
    30. }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hmm..where is the SetCountText method? :)
     
  5. pimentalamego

    pimentalamego

    Joined:
    Apr 9, 2018
    Posts:
    3
    Also when I took the code:
    Code (csharp):
    1. count = 0;
    2.         SetCountText();
    3.         winText.text = "";
    and tested the game, the player controller did not changed with the camera rotation.
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    from the code you provided there is no function called SetCountText. so for now you can comment it out or remove the line of code and thirdly you can create the function.

    // this is bare minimum needed to create the function., but comment it out is much easyer
    void SetCountText()
    {

    }