【问题标题】:How to fix this unity 2d sidescroller movement/animation problem如何解决这个统一的 2d 横向卷轴移动/动画问题
【发布时间】:2019-02-07 17:24:10
【问题描述】:

所以我正在统一创建我的第一个游戏,我目前正在研究从左到右的移动。

  • 我向左移动,松开左箭头按钮 -> 空闲动画。

  • 按向右箭头,角色向右移动,松开 -> 空闲。

问题是我必须等待空闲动画才能按下去选择另一个方向。当我按左和同时按右时,角色不会移动。

我想做的是fe:

  • 按左->字符向左,然后

  • 同时向右按 -> 字符向右移动。

因此需要能够同时按下按钮,并且最后按下的键决定了运动/动画。

动画代码:

if (Input.GetKeyDown(KeyCode.LeftArrow)) 
{
    this.GetComponent<Animator>().SetInteger("pallotila", 1);
}

if (Input.GetKeyUp(KeyCode.LeftArrow))
{
    this.GetComponent<Animator>().SetInteger("pallotila", 0);
}

if (Input.GetKeyDown(KeyCode.RightArrow))
{
    this.GetComponent<Animator>().SetInteger("pallotila", 2);
}

if (Input.GetKeyUp(KeyCode.RightArrow))
{
    this.GetComponent<Animator>().SetInteger("pallotila", 0);
}

玩家移动代码:

if (Input.GetKey(KeyCode.LeftArrow))
{
    this.transform.position += Vector3.left * this.nopeus * Time.deltaTime;
    this.transform.rotation = this.vasemmalle;
}

if (Input.GetKey(KeyCode.RightArrow))
{
    this.transform.position += Vector3.right * this.nopeus * Time.deltaTime;
    this.transform.rotation = this.oikealle;
}

【问题讨论】:

    标签: unity3d 2d


    【解决方案1】:

    请注意,您永远不要在Update 中使用GetComponent,最好只使用一次并重复使用该引用。

    //Here you store the Animator reference
    private Animator animator;
    
    private void Awake()
    {
        animator = GetComponent<Animator>();
    }
    

    我还使用switch 来定义哪个按钮应该发生什么,以避免一遍又一遍地使用相同的代码。

    private void SetLastPressed(KeyCode code)
    {
        int value = 0;
        switch (code)
        {
            case KeyCode.None:
                value = 0;
                break;
    
            case KeyCode.LeftArrow:
                value = 1;
                break;
    
            case KeyCode.RightArrow:
                value = 2;
                break;
        }
    
        animator.SetInteger("pallotila", value);
        lastPressed = code;
    }
    

    只需存储并检查最后按下的按钮,并使用if-else 使输入检查独占。

    // Here you store the last pressed key
    private KeyCode lastPressed = KeyCode.None;
    
    private void Update()
    {
    
        if (lastPressed != KeyCode.LeftArrow && Input.GetKeyDown(KeyCode.LeftArrow))
        {
            SetLastPressed(KeyCode.LeftArrow);
        }
        else if (lastPressed != KeyCode.RightArrow && Input.GetKeyDown(KeyCode.RightArrow))
        {
            SetLastPressed(KeyCode.RightArrow);
        }
    
        // If none of the keys is pressed reset
        else if (lastPressed != KeyCode.None && !Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.RightArrow))
        {
            SetLastPressed(KeyCode.None);
        }
    
        // And if only one of them is released but the other one still pressed
        //go on using that still pressed key again
        else if (lastPressed != KeyCode.LeftArrow && Input.GetKeyUp(KeyCode.RightArrow) &&
                 Input.GetKey(KeyCode.LeftArrow))
        {
            SetLastPressed(KeyCode.LeftArrow);
        }
        else if (lastPressed != KeyCode.RightArrow && Input.GetKeyUp(KeyCode.LeftArrow) &&
                 Input.GetKey(KeyCode.RightArrow))
        {
            SetLastPressed(KeyCode.RightArrow);
        }
    

    对于移动,您也可以简单地重复使用 lastPresses 值

        if(lastPressed == KeyCode.LeftArrow)
        {
            transform.position += Vector3.left * nopeus * Time.deltaTime;
            transform.rotation = vasemmalle;
        }
        else if (lastPressed == KeyCode.RightArrow)
        {
            transform.position += Vector3.right * nopeus * Time.deltaTime;
            transform.rotation = oikealle;
        }
    

    此外,您可以/应该使用List&lt;KeyCode&gt; 来存储最后一次印刷。每次按下按钮时,将按钮添加到列表的末尾;每当一个按钮出现时,将其从列表中删除

    result => 最后按下的按钮总是列表中的最后一个。

    这使得返回到上一个按钮的方式更加简单和灵活,而无需添加/更改如此多的代码行,例如

    private List<KeyCode> lastPresses = new List<KeyCode>();
    private KeyCode lastPressed = KeyCode.None;
    
    private Animator animator;
    
    private void Awake()
    {
        animator = GetComponent<Animator>();
    }
    
    private void SetLastPressed(KeyCode code)
    {
        int value = 0;
        switch (code)
        {
            case KeyCode.None:
                value = 0;
                break;
    
            case KeyCode.LeftArrow:
                value = 1;
                break;
    
            case KeyCode.RightArrow:
                value = 2;
                break;
        }
    
        animator.SetInteger("pallotila", value);
        lastPressed = code;
    }
    
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            if (!lastPresses.Contains(KeyCode.LeftArrow)) lastPresses.Add(KeyCode.LeftArrow);
        }
        else if (Input.GetKeyUp(KeyCode.LeftArrow))
        {
            if (lastPresses.Contains(KeyCode.LeftArrow)) lastPresses.Remove(KeyCode.LeftArrow);
        }
    
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            if (!lastPresses.Contains(KeyCode.RightArrow)) lastPresses.Add(KeyCode.RightArrow);
        }
        else if (Input.GetKeyUp(KeyCode.RightArrow))
        {
            if (lastPresses.Contains(KeyCode.RightArrow)) lastPresses.Remove(KeyCode.RightArrow);
        }
    
        var currentCode = lastPresses.Count > 0 ? lastPresses[lastPresses.Count - 1] : KeyCode.None;
    
        if (currentCode != lastPressed) SetLastPressed(currentCode);
    
        if (lastPressed == KeyCode.LeftArrow)
        {
            transform.position += Vector3.left * nopeus * Time.deltaTime;
            transform.rotation = vasemmalle;
        }
        else if (lastPressed == KeyCode.RightArrow)
        {
            transform.position += Vector3.right * nopeus * Time.deltaTime;
            transform.rotation = oikealle;
        }
    }
    

    【讨论】:

    • 现在除了在线 animator.SetInteger("pallotila", value);是错误“使用未分配的局部变量'值'。
    【解决方案2】:

    您可以将“值”变量更改为

        animator.SetInteger("pallotila", 0);
        break;
    

    在

    的位置
    value = 0;
    break;
    

    或改变

    int value;
    

    到

    int value = 0;
    

    在@derHugo 的代码中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多