《Dark Wonder》33集.巨魔的行走优化后,巨魔变成原地走路了。Unity5.3.5版本

张同学斯基

1 回复992 浏览

来自班级:Unity - A计划(永久有效期)

using UnityEngine;
using System.Collections;

public class troll : MonoBehaviour {
    public bool idle = true;
    private  float timer = 2.0f;
   // [Range(0.0f,10.0f)]
    public int speed =5;
    private Animator anim;
    private CharacterController controller;
    // Use this for initialization
    void Start () {
        this.transform.position = new Vector3(55, 100, 110);
        anim = GetComponent<Animator>();
        controller = GetComponent<CharacterController>();
    }
    // Update is called once per frame
	void Update () {
        timer -= Time.deltaTime;
        if (timer<=0)
        {
            if (idle )
            {
                //transform to move
                idle = false;
                timer = 5.0f;
                int temp = Random.Range(-90, 90);
                transform.Rotate(new Vector3(0, temp, 0));
                trollWalk();
            }
            else
            {
                //transoform to idle
                idle = true;
                timer = 2.0f;
                trollidle();
            }
        }
        if (!idle)
        {
            //how to move forward
            // transform.position += transform.forward * Time.deltaTime * speed;
            controller.SimpleMove(new Vector3(0,0,speed));
        }
    }
    public void trollWalk()
    {
        anim.SetFloat("run", 0.0F);
        anim.SetFloat("idle", 0F);
        anim.SetFloat("walk", 1.0F);
    }
    public  void trollidle()
    {
        anim.SetFloat("idle", 1F);
        anim.SetFloat("walk", 0.0F);
        anim.SetFloat("run", 0F);
    }
}

 

全部回复(1)

  • siki 1 楼

    在巨魔的animator组件面板上,把apply root motion取消勾选,这个意思是移动由动画控制

发表回复

登录后可以回复