角色移动
使用键盘输入(WASD或者↑↓←→)使角色移动的代码实现(本例子中有动画器,有两个状态(装备和不装备),可忽略)。
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
| void Start() { myAnimator = this.GetComponent<Animator>(); } void Update() { isEquipflag = this.GetComponent<Hero>().getIsEquiped(); Vector3 HeroCenterPoint = new Vector3( this.transform.position.x, this.transform.position.y + 1f, this.transform.position.z); if (Physics.Raycast(HeroCenterPoint, Vector3.down, out RHit, 5)) { if (RHit.collider.CompareTag("myTerrain")) { this.transform.position = new Vector3( this.transform.position.x, RHit.point.y, this.transform.position.z); } } if (isEquipflag) { isRun = false; if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { myAnimator.SetFloat("Walk", 1f); this.transform.forward = new Vector3( mainCamera.transform.forward.x, 0, mainCamera.transform.forward.z); } else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { myAnimator.SetFloat("Walk", 0.6f); this.transform.forward = new Vector3( mainCamera.transform.forward.x, 0, mainCamera.transform.forward.z); } else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) { myAnimator.SetFloat("Walk", 0f); this.transform.forward = new Vector3( mainCamera.transform.forward.x, 0, mainCamera.transform.forward.z); } else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) { myAnimator.SetFloat("Walk", 0.3f); this.transform.forward = new Vector3( mainCamera.transform.forward.x, 0, mainCamera.transform.forward.z); } else { stopWalk(); } } void stopWalk() { myAnimator.SetFloat("Walk", -0.2f); isRun = false; this.transform.forward = new Vector3( mainCamera.transform.forward.x, 0, mainCamera.transform.forward.z); } }
|
摄像机跟随
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
| using UnityEngine; using System.Collections;
public class MyFollow : MonoBehaviour {
public float distance; public float height; float LookAtPoint_X = 0f; float LookAtPoint_Y = 1.5f; float LookAtPoint_Z = 0f; float CurrentAngle = 0f; float Angle_X = 0f; float Angle_Z = 0f; RaycastHit RHit; public Transform Target;
void Start() { Angle_X = distance * Mathf.Cos((CurrentAngle - 60) / 180 * Mathf.PI); Angle_Z = distance * Mathf.Sin((CurrentAngle - 60) / 180 * Mathf.PI); LookAtPoint_X = Mathf.Cos((CurrentAngle + 60) / 180 * Mathf.PI); LookAtPoint_Z = Mathf.Sin((CurrentAngle + 60) / 180 * Mathf.PI); }
void Update() { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; float mouseX = Input.GetAxis("Mouse X"); float mouseY = Input.GetAxis("Mouse Y");
rotateCamera(mouseX * 2, mouseY * 2); } void LateUpdate() { if (!Target) return;
this.transform.position = Target.transform.position + new Vector3(Angle_X, height, Angle_Z);
this.transform.LookAt(Target.position + new Vector3(0, LookAtPoint_Y, 0));
Vector3 HeroCenterPoint = Target.transform.position + new Vector3(0, 1.5f, 0); if (Physics.Raycast(HeroCenterPoint, this.transform.position - HeroCenterPoint, out RHit, Vector3.Distance(this.transform.position, HeroCenterPoint))) { this.transform.position = RHit.point; }
CheckCameraCollision(this.transform.right, 0.5f); CheckCameraCollision(-this.transform.right, 0.4f);
CheckCameraCollision(this.transform.up, 0.4f); CheckCameraCollision(-this.transform.up, 0.3f); }
void CheckCameraCollision(Vector3 direction, float distance) { if (Physics.Raycast(this.transform.position, direction, out RHit, distance)) { this.transform.position = RHit.point - direction * 0.1f; } }
void rotateCamera(float deltaX, float deltaY) { float Mouse_X = deltaX / 5; float Mouse_Y = deltaY / 5;
CurrentAngle = (CurrentAngle - Mouse_X * 2 + 360) % 360;
Angle_X = distance * Mathf.Cos(CurrentAngle * Mathf.Deg2Rad); Angle_Z = distance * Mathf.Sin(CurrentAngle * Mathf.Deg2Rad);
LookAtPoint_X = Target.position.x + distance * Mathf.Cos(CurrentAngle * Mathf.Deg2Rad); LookAtPoint_Z = Target.position.z + distance * Mathf.Sin(CurrentAngle * Mathf.Deg2Rad); LookAtPoint_Y = Mathf.Clamp(LookAtPoint_Y + Mouse_Y * 0.1f, 0.5f, 3f);
height = Mathf.Clamp(height - Mouse_Y * 0.1f, 0.5f, 3f);
} public float getLookAtPoint_Y() { return LookAtPoint_Y; } }
|