简体   繁体   English

Unity动画

[英]Unity Animations

I am trying to change character animations using a script, based on key inputs, but Unity seems to only play the default "standing idle" animation and occasionally switching to the "crouched idle", is there a different way to handle animations or am I just doing the script wrong? 我试图基于脚本输入,使用脚本来更改角色动画,但是Unity似乎只播放默认的“站立闲置”动画,偶尔切换到“蹲下闲置”,是否有其他处理动画的方法?只是在做脚本错误? Here is my script as it currently stands 这是我目前的脚本

using UnityEngine;
using System.Collections;

public class CharacterControl : MonoBehaviour {

    private Animator animator;
    public bool crouched;
    private string sc;

    // Use this for initialization
    void Start () {

        animator = GetComponent<Animator> ();

    }

    // Update is called once per frame
    void Update () {

        if (crouched == true) {
            sc = "crouch";
        } else {
            sc = "standing";
        }

        animator.Play (sc + "_idle");

        if (Input.GetButton ("Fire3")) {
            if (crouched == false) {
                crouched = true;
            } else {
                crouched = false;
            }
        }

    }
}

Try replacing 尝试更换

if (Input.GetButton ("Fire3")) {
    if (crouched == false) {
        crouched = true;
    } else {
        crouched = false; 
    }
}

with

if (Input.GetButton ("Fire3")) {
    crouched = true;
} else {
    crouched = false;
}

Now, when you hold down the "Fire3" button, your character should crouch, and when you release it he/she should stand again 现在,当您按住“ Fire3”按钮时,角色应该蹲下,放开时,他/她应该再次站立

Also a suggestion: Put the other code in the function ( if (crouched == true) ... animator.Play (sc + "idle"); after this code (the Input.GetButton check). That way, your character should instantly start crouching the same frame that the button is pressed; otherwise, he/she will the frame after 还有一个建议:将其他代码放入函数中( if (crouched == true) ... animator.Play (sc + "idle");在此代码之后( Input.GetButton检查)。这样,您的角色应该立即开始蹲下与按下按钮相同的框架;否则,他/她将在


Explanation 说明

Input.GetButton will return true while you're pressing down (in the middle of clicking or touching) on the button each frame . 每帧按下(在单击或触摸的中间)按钮时, Input.GetButton将返回true。 Each time Update is called (around 1/60th of a second) your code will check if you're pressing down, and toggle crouched . 每次调用Update (约1/60秒),您的代码都会检查您是否按下了,并切换crouched When you click/tap the button, you'll likely be pressing down for a few frames, so crouched will switch from true to false , back and forth, a few times. 单击/点击按钮时,您可能会按下几帧,因此crouched几次会从true切换到false In some cases (when you press down for an odd number of frames) crouched will be switched, but in other cases (when you press down for an even number of frames) crouched will stay as it was before you clicked on the button, preventing your character from crouching, or standing if he was crouching before. 在某些情况下(当您按下奇数个帧时), crouched将被切换,但在其他情况下(当您按下奇数个帧时), crouched将保持在您单击按钮之前的状态。您的角色蹲下,或者如果他以前蹲过则站着。

Source: From the official API: Input.GetButton 来源:来自官方API: Input.GetButton

I would strongly suggest using animation states and transit from one to another when you get an input. 我强烈建议您使用动画状态,并在收到输入时从一个状态过渡到另一个状态。 Checkout my answer at: Unity 2D animation running partially 在以下位置查看我的答案: Unity 2D动画部分运行

Yes you can do the toggle action like this 是的,您可以像这样进行切换操作

void Update()
{
   if(Input.MouseButtonDown(2))
  {
    crouched = true;
  }
  if(Input.MouseButtonUp(2))
  {
    crouched  = false;
  }
}

您可以尝试以下代码:

crouched = Input.GetButtonDown("Fire3") ? true : false;

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

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