【问题标题】:Unity Animations统一动画
【发布时间】:2016-07-28 02:27:23
【问题描述】:

我正在尝试根据键输入使用脚本更改角色动画,但 Unity 似乎只播放默认的“站立空闲”动画,偶尔切换到“蹲下空闲”,是否有不同的方式来处理动画还是我只是做错了脚本?这是我目前的脚本

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

    }
}

【问题讨论】:

    标签: c# animation unity3d


    【解决方案1】:

    尝试替换

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

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

    现在,当你按住“Fire3”按钮时,你的角色应该蹲下,当你松开它时,他/她应该再次站立

    还有一个建议:将其他代码放入函数中(if (crouched == true) ... animator.Play (sc + "idle"); 在此代码之后(Input.GetButton 检查)。这样,您的角色应该立即开始蹲伏在按下按钮的同一帧;否则,他/she will the frame after


    说明

    Input.GetButton 将在您按下(在单击或触摸过程中)每一帧 按钮时返回 true。每次调用Update 时(大约1/60 秒),您的代码将检查您是否按下并切换crouched。当您单击/点击按钮时,您可能会按下几帧,因此crouched 将从true 切换到false,来回切换几次。在某些情况下(当您按下奇数帧时)crouched 将被切换,但在其他情况下(当您按下偶数帧时)crouched 将保持在您单击之前的状态按钮,防止你的角色蹲下,或者如果他之前蹲着的话就站起来。

    来源:来自官方API:Input.GetButton

    【讨论】:

    • 我喜欢这个主意,但我试图让蹲下成为一个切换动作,而不是按住按钮。有没有办法做到这一点?
    • 以前我的代码用 Input.GetButtonUp 代替了 Input.GetButton,但我在发布代码时忽略了将其改回
    【解决方案2】:

    我强烈建议您使用动画状态并在收到输入时从一个状态转换到另一个状态。查看我的答案:Unity 2D animation running partially

    【讨论】:

      【解决方案3】:

      是的,您可以像这样进行切换操作

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

      【讨论】:

      • 对于迟到的评论,我深表歉意,但我在注册状态更改方面没有问题,我实际上是在寻找如何切换动画而不是让它恢复为默认值。
      【解决方案4】:

      你可以试试这个代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 1970-01-01
        • 2016-04-16
        • 2014-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多