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

(196评价)
价格: 4039.00元
siki老师,我学习到《Dark Wonder》开发064课使用CharacterController控制巨魔行动时按照您的方法修改代码后,巨魔还是在原地打转,而且脚部分在地面下了
月色真美发起了问答2017-08-26
1
回复
464
浏览
using UnityEngine;
using System.Collections;

public class Troll : MonoBehaviour {
   public bool idle = true;
  public float timer = 2.0f;
  public float speed = 5.0f;
  private Animator anim;
  private CharacterController controller;

  private float angle = 0;
	// Use this for initialization
	void Start () {
        anim = GetComponent<Animator>();
        controller = this.GetComponent<CharacterController>();
	}
	
	// Update is called once per frame
	void Update () {
        //Vector3 forward = transform.TransformDirection(Vector3.forward);
        timer -= Time.deltaTime;
        if (timer <= 0)
        {
            if (idle)
            {
                transformToWalk();
            }
            else {
                transformToIdle();
            }
        }
        if (!idle)
        {
            if (Mathf.Abs(angle) >= 0.2f)
            {
                float temp = angle * 0.05f;
                transform.Rotate(new Vector3(0, temp, 0));
                angle -= temp;
            }
            //transform.position += transform.forward * Time.deltaTime * speed;
            controller.SimpleMove(transform.forward * speed);
        }
	}
    public void transformToWalk()
    {
        idle = false;
        timer = 5.0f;

        angle = Random.Range(-90, 90);
        //int temp = Random.Range(-90, 90);
        //transform.Rotate(new Vector3(0, angle, 0));
        animationToWalk();
    }

    public void transformToIdle()
    {
        idle = true;
        timer = 2.0f;
        animationToIdle();
    }

    public void animationToWalk()
    {
        anim.SetFloat("run", 0.0F);
        anim.SetFloat("idle", 0F);
        anim.SetFloat("walk", 1.0F);
    }
    public void animationToIdle()
    {
        anim.SetFloat("idle", 1F);
        anim.SetFloat("walk", 0.0F);
        anim.SetFloat("run", 0F);
    }
}

 

所有回复
  • siki 2017-08-26

    巨魔身上有一个anmator组件,这个animator身上有一个apply root motion 取消勾选了,可能是这个参数影响了没办法控制移动

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