Home > other >  Moving foward to the face direction
Moving foward to the face direction

Time:11-29

I have player charactor which has face(head) and body.

Player - Head
       - Body

Currently my source code attached to the player is like this,

public void moveForward(){
    Debug.Log("move forward");
    speed = 40;
    Debug.Log("Head Rotation y:"   head.transform.localEulerAngles.y); // For example it shows 30
    float y_rotation = head.transform.localEulerAngles.y;
    this.transform.Translate(0,0,speed/50);// It goes to the player direction, how can I move forward to the face direction???
}

This function makes character move forward, but it comes to the player direction.

I would like to move forward to the face direction, How can I make this??


Updated article

public void moveForward(){
    Debug.Log("move forward");
    speed = 40;
   Debug.Log("head.transform.forward.x:"  head.transform.forward.x);//1
    Debug.Log("head.transform.forward.y:"  head.transform.forward.y);//0
    Debug.Log("head.transform.forward.z:"  head.transform.forward.z);// -1.192093E-07

    Debug.Log("this.transform.forward.x:"  this.transform.forward.x);0
    Debug.Log("this.transform.forward.y:"  this.transform.forward.y);0
    Debug.Log("this.transform.forward.z:"  this.transform.forward.z);1


//somehow this two do the same move,,
    this.transform.position  = head.transform.forward * vz / 50f * Time.deltaTime;
    //this.transform.position  = this.transform.forward * vz / 50f * Time.deltaTime;

}

CodePudding user response:

To move in the direction the head is facing simply do

transform.position  = head.transform.forward * speed / 50f * Time.deltaTime;
  • Related