今天明白了如何使用CharactorController来控制对象移动都方法,将其记录下来。也可以给和我一样都菜鸟们一起学习分享,如果有人看到了文章,请给给评论什么的,给我点动力嘛。
CharactorController实现都移动其实很简单,调用Move方法,对象就能按照你穿都向量来移动,但是它没有帮你封装终点,所以只能自己去计算,你离终点都距离,如果你离终点都距离小于了你可以接受都距离,那么你就可以停止了值,这次都移动,这样你都角色也就走到了你需要都目的地。
直接上代码吧。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
using UnityEngine; using System.Collections; /// <summary> /// 简单都角色控制器 /// </summary> public class PlayerControl : MonoBehaviour { //鼠标点击特效 public Transform effect; //摄像头环绕角色的角度 public float angleX = 0; public float angleY = 0; public float angleZ = 0; //摄像头环绕角色都距离 public float distanceX = 0; public float distanceY = 0; public float distanceZ = 0; //速度 public float speed = 10f; //目标点与当前点之间都距离 private float distX; private float distZ; private float dist; private CharacterController character; private Transform m_camera; //目标点坐标 private Vector3 m_targetPosition; //目标点角度 private Quaternion m_targetRotation; //是否开始移动 private bool startMove = false; //移动偏移距离 private Vector3 step; void Awake() { character = GetComponent<CharacterController>(); m_camera = Camera.main.transform; } void Update () { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; //发送射线,只有地型接收射线 if (Physics.Raycast(ray, out hit, 1 << LayerMask.NameToLayer("Ground"))) { m_targetPosition = Vector3.zero; m_targetPosition.x = hit.point.x; m_targetPosition.y = transform.position.y;//hit.point.y; //设置为自身都y那么在爬坡都时候,角色不会倾斜,如果改为hit.point.y,角色就会倾斜. m_targetPosition.z = hit.point.z; //得到目标点与当前点之间都角度 m_targetRotation = Quaternion.LookRotation(m_targetPosition - transform.position); //创建鼠标点击地面特效 Instantiate(effect, new Vector3(hit.point.x, hit.point.y + 0.05f, +hit.point.z), Quaternion.identity); //开始移动角色 startMove = true; } } Move(); Follow(); } /// <summary> /// 角色移动方法 /// </summary> private void Move() { if (startMove) { //判断角度,如果角度不同,则旋转角度 if (!transform.rotation.Equals(m_targetRotation)) { transform.rotation = Quaternion.Lerp(transform.rotation, m_targetRotation, Time.deltaTime * 20); } //计算距离 distX = transform.position.x - m_targetPosition.x; distZ = transform.position.z - m_targetPosition.z; dist = distX * distX + distZ * distZ; //如果距离小于0.2那么停止移动 if (dist > 0.2f) { step = Vector3.Lerp((m_targetPosition - transform.position).normalized, transform.forward, Time.deltaTime) * speed; Vector3 motion = step * Time.deltaTime; motion.y = -30; character.Move(motion); } else { startMove = false; } } } /// <summary> /// 摄像头跟随方法 /// </summary> private void Follow() { Quaternion rotation = Quaternion.Euler(angleX, angleY, angleZ); Vector3 Pos = new Vector3(distanceX,distanceY,distanceZ); Vector3 move = rotation * Pos; m_camera.position = transform.position + move; m_camera.rotation = rotation; } } |
2 Comments
is good job.
thanks!