Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4039.00元
我在使用runtimeAnimatorController切换动画状态机的时候,每切换一次,玩家的位置就又回到初始位置了
赵宇发起了问答2017-08-04
4
回复
4743
浏览
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Animations;
using UnityEngine;

//所有的动画状态机的名称
enum AnimationIndex {
    Run_SH,
    Walk_SH
}



public class PlayerAnimation : MonoBehaviour {
   
    public List<AnimatorController> allAnimators = new List<AnimatorController>();
    private  Animator playerAnimator;
    public  int  animatorIndex=0;//这个是控制到底当前使用的是哪个动画控制器


    //声明动画状态单元集合
    AnimatorState[] states = new AnimatorState[2];
	// Use this for initialization
	void Start () {
        playerAnimator = GetComponent<Animator >();
        playerAnimator.runtimeAnimatorController = allAnimators[0];

        // allAnimators      
        // Debug.Log(allAnimators [0].name );
    }

    //public float time = 3;
    //private float timer = 0;

	// Update is called once per frame
    private Vector3  pos;

	void Update () {

        if (Input .GetKey (KeyCode.B )) {
            if (animatorIndex == 0)
            {            
                animatorIndex = 1;  
            }
            else {
              
                animatorIndex = 0;
            }
        }
        //在这里执行对应的播放动画
        AnimtorIndex(animatorIndex);
    }

    //通过枚举来判断当前该使用什么动画状态机
    private void AnimatorNow(AnimationIndex animator)
    {
        switch (animator)
        {
            case AnimationIndex.Run_SH:
                //问题可能出在切换动画控制器上了。
                playerAnimator.runtimeAnimatorController = allAnimators[1];
               
                //PlayerMove.instance.transform.Translate (Vector3 .forward );
                  SH_Animation.instance.Run_SHAnimation(PlayerMove.instance .h, PlayerMove.instance.v);
                break;
            case AnimationIndex.Walk_SH:
                //我们在切换动画控制器的时候角色的位置又会回到初始位置,难道是动画控制器本身带有Tranform属性信息???
                playerAnimator.runtimeAnimatorController = allAnimators[0];
               
               // PlayerMove.instance.transform.Translate(Vector3.forward);
                SH_Animation.instance.Walk_SHAnimation(PlayerMove.instance.h, PlayerMove.instance.v);
                break;
        }
    }


    //这是一个判断角色正在使用什么动作(走路或者跑步)的方法。
    public void AnimtorIndex(int  animatorIndex) {
        switch (animatorIndex) {
            case 0:
                AnimatorNow(AnimationIndex.Walk_SH);
                break;
            case 1:
                AnimatorNow(AnimationIndex.Run_SH );
                break;
        }
    }

}

关键问题出在了 playerAnimator.runtimeAnimatorController = allAnimators[1];

我试过了,只要按下B键,角色的动画控制器被切换之后,不管角色的坐标位置在哪,都会跑到最初的位置上去。这个怎么解决啊?

 

所有回复
  • siki 2017-08-04

    在animator身上有一个apply root motion,把这个取消勾选了试试

    一个状态机不能满足需求吗,还要使用两个来回切换

    • 赵宇 2017-08-07

      已经取消了,没有用。一个状态机肯定不够,如果我切换不同的武器,或者是同一个武器切换走路和跑步的动作,比如向左走向右走,变成向左跑向右跑,端着枪跑和手里拿着刀跑或者走,大量的动作只放在一个状态机里面人会疯掉的

      (0) 回复
    • 赵宇 2017-08-07

      老师,我看了一下这个文章,虽然值得借鉴,但是还是解决不了我的问题http://blog.csdn.net/langresser_king/article/details/37760091

      (0) 回复
    还有-3条回复,点击查看
    你还没有登录,请先登录注册
  • 赵宇 2017-08-07

    老师,apply root motion,我取消勾选出了问题,我把勾选勾上就好了,这块问题解决啦!谢谢老师

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!