动画状态切换

 

    Animator anim = this.GetComponent<Animator>();

    AnimatorStateInfo animatorInfo;
        animatorInfo = anim.GetCurrentAnimatorStateInfo(0);
        if (IsOk)
        {
           
            anim.SetTrigger(str);
        }

        if ((animatorInfo.normalizedTime > 1) && (animatorInfo.IsName(str)))
        {

            
            IsOk = true;
            //print("动画播放完了");
            GameManager._Instance.IsPlay = false;
            GameManager._Instance.OpenRay();

        }
        else
        {
            IsOk = false;
            GameManager._Instance.CloseRay();
            //print("动画正在播放中");
        }
        

        
    
 

转载于:https://my.oschina.net/u/2874878/blog/757082

以下是一种可能的Unity代码,用于控制角色移动时的动画状态切换: ```c# using UnityEngine; public class CharacterController : MonoBehaviour { private Animator animator; private Rigidbody2D rb; private bool isMoving; private float moveSpeed; private float horizontalInput; private void Start() { animator = GetComponent<Animator>(); rb = GetComponent<Rigidbody2D>(); } private void Update() { // 获取水平输入 horizontalInput = Input.GetAxis("Horizontal"); // 根据移动速度,判断角色是否移动中 isMoving = Mathf.Abs(horizontalInput) > 0; // 更新动画参数 animator.SetBool("isMoving", isMoving); // 根据水平输入调整角色朝向 if (horizontalInput > 0) { transform.localScale = new Vector3(1, 1, 1); } else if (horizontalInput < 0) { transform.localScale = new Vector3(-1, 1, 1); } } private void FixedUpdate() { // 设置角色的水平移动速度 moveSpeed = 5f; // 可根据需要调整速度 rb.velocity = new Vector2(horizontalInput * moveSpeed, rb.velocity.y); } } ``` 在这个示例代码中,`CharacterController` 脚本绑定在角色游戏对象上,用于控制角色的移动和动画状态切换。在 `Start` 方法中,我们获取了角色的 `Animator` 组件和 `Rigidbody2D` 组件。 在 `Update` 方法中,我们首先通过 `Input.GetAxis` 获取水平输入,然后通过判断水平输入的绝对值判断角色是否在移动中,并将这个状态值更新到动画参数 `isMoving` 中。接着,我们根据水平输入的正负调整角色的朝向,从而实现角色的左右移动。 在 `FixedUpdate` 方法中,我们根据水平输入设置角色的水平移动速度,并通过给 `Rigidbody2D` 组件的 `velocity` 属性赋值来实现角色的物理移动。 通过这样的实现,我们可以在角色移动时动态切换不同的动画状态,从而呈现出流畅的角色移动效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值