简体   繁体   English

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

[英]how to make player in direction, in which he walking unity3d

my game is top down view, I am able to move character through joystick, now I want to make character looks in direction he walking to...don't know where to start, and write code, any suggestions here is my code.. 我的游戏是自上而下的视图,我能够通过操纵杆移动角色,现在我想使角色朝他走路的方向看...不知道从哪里开始,并编写代码,这是我的代码。 。

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)
}

edit : transform.LookAt (movement); 编辑:transform.LookAt(运动); // I tried this, but character looks in mid only //我试过了,但是角色看起来只在中间

LookAt "looks" at a position, not a direction. LookAt会“看”某个位置,而不是方向。 Your movement is a direction relative to player position. 您的移动是相对于玩家位置的方向。 Try this: 尝试这个:

transform.LookAt (transform.position + movement.normalized);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM