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

Move player forward based on it's y rotation

Discussion in 'Scripting' started by nicklowkc1, Oct 30, 2020.

  1. nicklowkc1

    nicklowkc1

    Joined:
    Feb 27, 2019
    Posts:
    28
    I am currently working on a simple three person player movement which player moves based on its rotation. I able to get my camera facing player based on player's rotation but I couldn't get my player move based on the rotation it's facing. I don't want to use unity provide methods like transform.TransformDirection because I am trying to see how well I understand the trigonometry behind it.

    The following is my code:
    Code (CSharp):
    1. void thirdPersonMovement()
    2.     {
    3.    
    4.         yRotation = transform.eulerAngles.y;
    5.         float xMove = unitDistance * Mathf.Sin( yRotation * Mathf.Deg2Rad) * 0.01f;
    6.         float zMove = unitDistance * Mathf.Cos( yRotation * Mathf.Deg2Rad) * 0.01f;
    7.  
    8.         Vector3 forwardVector = new Vector3(xMove, 0, zMove);
    9.         Debug.Log(forwardVector);
    10.         if(Input.GetKey(KeyCode.UpArrow))
    11.             transform.Translate(forwardVector);
    12.      
    13.     }
    Could someone guide me which part I did it in the wrong way?
    Thanks.
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
  3. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    Where is the problem? Are you getting any error ?
    I made a quick test for the code. Seems to be working fine for me.
     
  4. nicklowkc1

    nicklowkc1

    Joined:
    Feb 27, 2019
    Posts:
    28
    i have another three person camera which faces where the player is facing. when i am using the controller code, for example the player moves forward if the y rotation is 0 degree. However if I slightly rotate it to like 65 or bigger, the forward vector became weird. i am not sure if thats my camera problem but when i try to edit player rotation on scene editor it seems the weird thing didnt change.
     
  5. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    Ok.
    Try changing Translate's 2nd parameter to Space.World.
    May be that's the issue you are talking about.
     
  6. nicklowkc1

    nicklowkc1

    Joined:
    Feb 27, 2019
    Posts:
    28
    Hi. Thank you for your reply. I will try that when I reach home, don't have computer on my hands now.
    Just want to make sure one thing, is my calculation correct? I am trying to implement something like transform.forward to understand the math behind. But it seems like the math done in the following page is a little bit different
    https://forum.unity.com/threads/wha...lating-transform-forward-of-an-object.521318/
     
  7. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    I will check the link later.
    However I tested the code. And I see the gameObject moving at an angle, it's rotated at. So calculations look fine in my opinion.
    And using Space.World instead of Space.Self and vice versa does make difference in output.
     
    nicklowkc1 likes this.
  8. nicklowkc1

    nicklowkc1

    Joined:
    Feb 27, 2019
    Posts:
    28
    Hi, I tried it today and it works!
    I believe my math done it right way. Thank you!
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    Your code is framerate-dependent, ie. the player will move faster at faster framerates.
    You're also using the euler angles of the transform, which isn't very accurate. As @orionsyndrome is saying, just use your transform's .forward vector, which is what you're calculating, just faster and more convenient.
     
    orionsyndrome likes this.
  10. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    transform.forward
    is literally the same thing as
    transform.rotation * Vector3.forward

    only more convenient and, perhaps, faster.