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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Player body not rotate when head rotate

Discussion in 'Scripting' started by three2wan, Jan 1, 2016.

  1. three2wan

    three2wan

    Joined:
    Jul 28, 2015
    Posts:
    25
    Hi and Happy New Year to my fellow developers,

    I having problem with player rotation, I'm using google cardboard sdk for my main camera and attach to my custom player. When ever my player looking left or right, the body not rotate together with the head and if I try to move forward, the player will move in world coordinate instead of local coordinate of the player. Is like no matter where I'm looking, i move at same one direction. How do I fix that?

    This is my player controller script.

    Code (csharp):
    1.  
    2. public class FPController : MonoBehaviour {
    3.     public float speed = 5.0f;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.        
    8.     }
    9.    
    10.     // Update is called once per frame
    11.     void Update () {
    12.        
    13.         float moveForward = Input.GetAxis("Vertical") * speed;
    14.         float moveSide = Input.GetAxis("Horizontal") * speed;
    15.  
    16.         Vector3 moveSpeed = new Vector3(moveSide, 0f, moveForward);
    17.  
    18.         CharacterController cc = GetComponent<CharacterController>();
    19.  
    20.         cc.SimpleMove(moveSpeed);
    21.     }
    22. }
    23.  
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Don't you want the player to always move in the direction that the camera is facing & not the direction the capsule is facing? If so the capsule needs to rotate when the player is looking side to side, sort of like if you had no neck the only way you can turn to see something is to rotate your whole body.
     
  3. three2wan

    three2wan

    Joined:
    Jul 28, 2015
    Posts:
    25
    Actually the one that you suggest is that I want. Right now the way my player move forward depend on capsule is facing not the camera is facing. I want they move forward depend on camera facing.

    I'm sorry my english is so bad that hard to you guys to understand.
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Your English is fine, I just wanted to make sure I knew what you asked.

    The way I'd do it is child the camera to the capsule so the axis of both line up & are the same so that forward on the camera is forward on the capsule etc. Use the horizontal-axis to change the rotation around the Y-axis & use the vertical-axis to move the character by vector3.forward*speed.

    This may not be the best way though.
     
    Ezzaefah190919 and three2wan like this.
  5. three2wan

    three2wan

    Joined:
    Jul 28, 2015
    Posts:
    25

    Geez thanks! I finally made it. Thanks alot with your advise.
     
    Ezzaefah190919 and tedthebug like this.
  6. Ezzaefah190919

    Ezzaefah190919

    Joined:
    Dec 4, 2018
    Posts:
    1
    hi, I have the same problem with the player rotation.
    upload_2018-12-4_17-58-40.png
    can u help me with my codes? I just don't understand cos i'm a beginner in Unity T_T. Please help me. Thank you.
    upload_2018-12-4_18-0-14.png upload_2018-12-4_18-1-32.png