运动控制(2D场景)

时间:2014-04-09 03:29:21

标签: c# unity3d game-physics

我的动作代码存在问题我正在为2D中的自上而下的RPG写作。原谅我缺乏知识,但我最近才学习如何编码,这是我第一次尝试运动控制。期望的结果是有两种输入方法;控制器和键盘。我想让它们分开,所以当你使用控制器时,你不能使用键盘,反之亦然。这是代码。

using UnityEngine;
using System.Collections;

public class playerMovement : MonoBehaviour 
{   

    public float Speed = 1;
    private float deadZone = 0.25f;    

    // START - Use this for initialization
    void Start () {}

    // UPDATE is called once per frame - Using deadZone to compensate for faulty controllers etc.... Using options for controller true or false to switch between keyboard and controller input.
    void FixedUpdate () 
    {

        Vector2 stickInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

        if ( Options.useController == true)
        {
            if( Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A))
            {
                stickInput = Vector2.zero;
            }
            else if ( stickInput.magnitude < deadZone)
            {
                stickInput = Vector2.zero;
            }
            else
            {
                stickInput.Normalize();
                rigidbody2D.velocity = stickInput * Speed * Time.deltaTime;
            }

        }

        //Here is keyboard input - this is working great.
        else if ( Options.useController == false)
        {
            if ( Input.GetKey(KeyCode.A) && Input.GetKey (KeyCode.W))
            {
                stickInput = new Vector2(-1f,1f);
                stickInput.Normalize();
                rigidbody2D.velocity = stickInput * Speed * Time.deltaTime;  
            }
            else if ( Input.GetKey(KeyCode.W) && Input.GetKey (KeyCode.D))
            {
                stickInput = new Vector2(1f,1f);
                stickInput.Normalize();
                rigidbody2D.velocity = stickInput * Speed * Time.deltaTime; 
            }
            else if ( Input.GetKey(KeyCode.S) && Input.GetKey (KeyCode.D))
            {
                stickInput = new Vector2(1f,-1f);
                stickInput.Normalize();
                rigidbody2D.velocity = stickInput * Speed * Time.deltaTime; 
            }
            else if ( Input.GetKey(KeyCode.A) && Input.GetKey (KeyCode.S))
            {
                stickInput = new Vector2(-1f,-1f);
                stickInput.Normalize();
                rigidbody2D.velocity = stickInput * Speed * Time.deltaTime; 
            }
            else if ( Input.GetKey(KeyCode.W))
            {
                stickInput = new Vector2(0f,1f);
                stickInput.Normalize();
                rigidbody2D.velocity = stickInput * Speed * Time.deltaTime; 
            }
            else if ( Input.GetKey(KeyCode.D))
            {
                stickInput = new Vector2(1f,0f);
                stickInput.Normalize();
                rigidbody2D.velocity = stickInput * Speed * Time.deltaTime; 
            }
            else if ( Input.GetKey(KeyCode.S))
            {
                stickInput = new Vector2(0f,-1f);
                stickInput.Normalize();
                rigidbody2D.velocity = stickInput * Speed * Time.deltaTime; 
            }
            else if  ( Input.GetKey(KeyCode.A))
            {
                stickInput = new Vector2(-1f,0f);
                stickInput.Normalize();
                rigidbody2D.velocity = stickInput * Speed * Time.deltaTime; 
            }       
        }




    }
}

顶循环执行时问题到了。当useController等于true时,我希望从键移动为空。只要按下键,它就会很好用,但只要释放键(A,D,S,W),它就会退出if语句,并且播放器会被剩余的GetAxis值移动。 / p>

我必须在此循环中使用GetAxis作为控制器输入,不幸的是Unity会自动从键中获取值。有没有办法我不仅可以将速度限制为0,还可以确保当释放键时GetAxis值也变为0。

1 个答案:

答案 0 :(得分:0)

您必须使用rigidbody2d.velocity将要更改的对象附加到rigidbody2d。但是,每帧都这样做并不是一个好主意。相反,您可以使用transform.translate,或者如果您想通过使用刚体保持物理模拟,请使用rigidbody2d.addforce。

此外,在编辑&gt;项目设置&gt;输入确保从轴上移除WSAD键。

您的代码看起来应该更像

void FixedUpdate () 
{

    if (Options.useController)
    {
        // if the controller is enabled do this stuff
        Vector2 stickInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    }
    else
    {
        // else if the controller is not enabled, do this stuff.
        stickinput = new Vector2(0, 0);
        if (Input.GetKey(KeyCode.W)
            stickinput += new Vector2(0, 1);
        if (Input.GetKey(KeyCode.S)
            stickinput += new Vector2(0, -1);
        if (Input.GetKey(KeyCode.A)
            stickinput += new Vector2(-1, 0);
        if (Input.GetKey(KeyCode.D)
            stickinput += new Vector2(1, 0);
        /* can have code like:
         * if (stickinput.x > 0)
         *     animator.crossfade("rightAnimation", 0f);
         * else if (stickinput.x < 0)
         *     animator.crossfade("leftanimation", 0f);
         * else
         *     animator.crossfade("standinganimation", 0f);
         */
    }

        transform.Translate(((stickinput.magnitude > 1) ? stickinput.normalized : stickinput) * speed * Time.fixedDeltaTime, Space.Self);

        // or use the one commented out below
        // rigidbody2D.AddForce(((stickinput.magnitude > 1) ? stickinput.normalized : stickinput) * speed * Time.fixedDeltaTime);


}

注意我没有使用if if。如果有人持有W和S,则y组件的值为0。我选择Space.Self进行本地翻译,但你可以使用Space.World来移动它相对于全局轴。