流程
创建人物,创建控制器,将控制器拉入人物的Animator中。
点击控制器,创建参数Bool类型,创建状态Run和Idle,并导入相应的动画。
其中
人物身上创建代码。
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMoveS02 : MonoBehaviour {
//public在外面可看到,private在外面看不到
private CharacterController cCtrl;
private Vector3 direction;
// 若不是视角随着物体移动,下面这句可删除。
public Transform target;
public float Speed = 5;
private bool isRun;
// Use this for initialization
void Start() {
cCtrl = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update() {
direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//SimpleMove 不接受高度变化,只接受水平变化。
//transform.LookAt 转向看的位置
cCtrl.SimpleMove(direction * Speed);
// transform.LookAt(target);视角对着物体
//或是 transform.LookAt(GameObject.Find("Cube").transform);
transform.LookAt(transform.position + direction);
//if else 删掉 变成isRun=direction.manitude !=0也可以
//isIdle=direction.manitude==0 GetComponent<Animator>().SetBool("isRun",!isIdle);
if (direction.magnitude==0)
{
isRun = false;
}
else
{
isRun = true;
}
GetComponent<Animator>().SetBool("isRun",isRun);
}
}
运行