角色移动

使用键盘输入(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)
{
// 将鼠标移动量除以5,减小旋转速度
float Mouse_X = deltaX / 5;
float Mouse_Y = deltaY / 5;

// 更新当前角度,确保它在0到360度之间
CurrentAngle = (CurrentAngle - Mouse_X * 2 + 360) % 360;

// 根据当前角度更新摄像机的位置
Angle_X = distance * Mathf.Cos(CurrentAngle * Mathf.Deg2Rad);//Mathf.Deg2Rad11取弧度
Angle_Z = distance * Mathf.Sin(CurrentAngle * Mathf.Deg2Rad);

// 更新摄像机注视点的坐标,distance相当于圆的半径,这是相对于target的坐标
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;
}
}