如何使玩家朝着正确的方向前进

时间:2018-11-19 11:45:57

标签: c# unity3d

我的游戏是自上而下的视图,我能够通过操纵杆移动角色,现在我想使角色看起来朝着他走路的方向...不知道从哪里开始,并编写代码,这里的任何建议是我的代码。

void FixedUpdate()
{
    var inputDevice = InputManager.ActiveDevice;

    if (inputDevice != InputDevice.Null && inputDevice != TouchManager.Device)
    {
        TouchManager.ControlsEnabled = false;
    }
    float horizontal = inputDevice.Direction.X;
    float vertical = inputDevice.Direction.Y;
    if (cam != null) //if there is a camera
    {
        camForward = Vector3.Scale(cam.up, new Vector3(1, 0, 1)).normalized;
        move = vertical * camForward + horizontal * cam.right;
    }
    else
    {
        move = vertical * Vector3.forward + horizontal * Vector3.right;
    }

    if (move.magnitude > 1) //Make sure that the movement is normalized
        move.Normalize();

    Move(move);
    Vector3 movement = new Vector3(horizontal, 0, vertical);    
    if (horizontal == 0 && vertical == 0)
    {
        rigidBody.velocity = Vector3.zero * 0.5f;
    }


    rigidBody.AddForce(movement * speed / Time.deltaTime);
    transform.LookAt (movement); // I tried this, but character looks in mid only (0,0.4,0)
}

编辑:transform.LookAt(运动); //我试过了,但是角色看起来只在中间

1 个答案:

答案 0 :(得分:1)

LookAt“看”到一个位置,而不是一个方向。您的移动是相对于玩家位置的方向。试试这个:

hwi_oauth