| using System.Collections; |
| public class heroControl : MonoBehaviour { |
| CharacterController hero; |
| public Transform[] points; |
| public int moveSpeed = 10; |
| hero = GetComponent<CharacterController>(); |
| //如果主角距离点的距离大于0.2,则算出主角的朝向,移动主角人物 |
| if (Vector3.Distance(ignoreY(points[nextIndex % points.Length].position), ignoreY(transform.position)) > 0.2f) |
| Vector3 direction = (ignoreY(points[nextIndex % points.Length].position) - ignoreY(transform.position)).normalized; |
| //插值改变主角的朝向,使其有一个自然转向的过程,防止其瞬间转向 |
| hero.transform.forward = Vector3.Lerp(transform.forward, direction, 0.1f); |
| hero.SimpleMove(transform.forward * moveSpeed); |
| //这个函数用来取消向量的Y轴影响,比如主角的高度与点之间可能有一段距离,我们要忽略这段距离 |
| Vector3 ignoreY(Vector3 v3) |
| return new Vector3(v3.x, 0, v3.z); |
|