Search Unity

Player move direction help

Discussion in 'Scripting' started by Mafi3089, Oct 17, 2019.

  1. Mafi3089

    Mafi3089

    Joined:
    May 12, 2019
    Posts:
    1
    Code (CSharp):
    1.         moveDirection = transform.TransformDirection(moveDirection);
    2.          moveDirection = (target - transform.position).normalized;
    3.         moveDirection.y = 0;
    4.         float myDirection = transform.rotation.y;
    5.         Vector3 vectorDirection = Quaternion.Euler(0, myDirection, 0) * moveDirection;
    6.         //moveDirection = transform.TransformDirection(Vector3.right);
    7.         playerMove.MovePlayer(vectorDirection.x, vectorDirection.z);
    8.  
    Новый точечный рисунок.jpg
    need to convert direction based on current player rotation
    like that:
    Code (CSharp):
    1. moveDirection -= transform.rotation.y
    Новый точечный рисунок.jpg
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    transform.rotation.y
    doesn't mean what you think it does and you basically shouldn't use it under any circumstances ever (transform.rotation is a Quaternion, and the w/x/y/z parameters of a Quaternion have nothing directly to do with the x/y/z axes). You are probably thinking of
    transform.eulerAngles.y
    , though using that is still rather fraught.

    If your goal is to get the direction directly behind the player, you should probably be using
    -transform.forward
    instead.