unity3d - 如何在动画师统一中停止特定动画?
问题描述
我想在按下一个键时循环播放特定的动画,并希望它在释放同一个键时停止。怎么做?这是我想要动画和移动的角色的 Unity2D 代码。不要巨魔我是一个 14 岁的新手。我的下面的代码不能完成这项工作。//跳过 StackOverflow 最少单词的东西 asda dfasdgaga asfgafa gsghagaggasgagasdgasgasdgagasggjdfhjdfgnudy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermovement : MonoBehaviour
{
// Animator
Animator m_Animator;
public float runSpeed = 10f;
public float Jumpspeed = 50f;
void Start()
{
m_Animator = gameObject.GetComponent<Animator>();
}
//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
void FixedUpdate()
{
// THE Shortcut SHIT
KeyCode RightKey = KeyCode.D; //shortcut
KeyCode LeftKey = KeyCode.A; //shortcut
KeyCode jump = KeyCode.Space; //shortcut
// BUTTON PRESSED AND ANIMATION Bitch
if (Input.GetKey(RightKey)) //when Right Key pressed (D)
{
Right();
}
else if (Input.GetKey(LeftKey)) //when Left Key pressed (A)
{
Left();
}
else if (Input.GetKey(jump)) //when (space) JUMP is pressed
{
Jump();
}
}
// THE MOVEMENTS SHIT
public void Right()
{
KeyCode RightKey = KeyCode.D; //shortcut
GetComponent<Rigidbody2D>().AddForce(Vector2.right * runSpeed);
if (Input.GetKeyDown(RightKey))
{
m_Animator.Play("Rightanim");
}
else
{
m_Animator.PlayInFixedTime("Rightanim", 1, 0.0f);
}
}
public void Left()
{
GetComponent<Rigidbody2D>().AddForce(Vector2.left * runSpeed);
m_Animator.Play("leftanim");
}
public void Jump()
{
GetComponent<Rigidbody2D>().AddForce(Vector2.up * Jumpspeed);
m_Animator.Play("jumpanim");
}
}
解决方案
首先为您的 playermovement 定义另一个刚体组件以避免 GetComponent().AddForce(Vector2.up * Jumpspeed); 在每次按键时,GetComponent() 都会产生更新成本。定义一个刚体2D _rb;进去
start()
{
_rb = GetComponent<Rigidbody2D>()
}
对于您的动画,您可以在动画器(窗口 > 动画 > 动画器)中为每个移动键(如 RightSpeed、LeftSpeed)定义浮点参数。
然后将转换条件添加到右动画并使用此选择条件行的浮动 RightSpeed 值转换回先前的动画并添加 RightSpeed 参数。选择更大并将其设为 0.1。并且折返条件小于1。
改变
if (Input.GetKeyDown(RightKey))
{
m_Animator.setfloat("RightSpeed", 1f);
}
// and add an another if check
if (Input.GetKeyUp(RightKey)) // this get key up trigger when user stop holding key
{
m_Animator.setfloat("RightSpeed", 0f);
}
推荐阅读
- python - 熊猫数据框按列值的下一次出现分组
- angular - 用于动画的 Angular CSS3 关键帧
- javascript - 发布请求后,继续检查是否完成 | 节点.js
- javascript - 用户只能在 Firestore 上读取/写入自己的提交
- jquery - Jquery在页面加载后自动点击
- kubernetes - 如何在 kops 上禁用 --basic-auth-file?Kubernetes
- ssis - 将条件拆分的结果传递给存储过程
- javascript - 过滤掉某个字符串
- django - Django url 操作 - 带有 pk / id 的详细信息页面不应基于该对象的属性显示 - 最佳实践
- networking - 如果使用本地流量策略,Kubernetes 负载均衡器会停止服务流量