这是控制角度代码,请加上移动逻辑
时间: 2024-03-02 20:49:37 浏览: 12
好的,以下是加上移动逻辑的代码:
```
Vector3 Rotating(float horizontal, float vertical)
{
// 获取相机前向方向,忽略掉垂直方向的分量,即相机的上下方向
Vector3 forward = playerCamera.TransformDirection(Vector3.forward);
forward.y = 0.0f;
forward = forward.normalized;
// 根据相机前向方向和玩家输入的移动方向计算目标方向
Vector3 right = new Vector3(forward.z, 0, -forward.x);
Vector3 targetDirection = forward * vertical + right * horizontal;
// 如果目标方向不为零
if (targetDirection != Vector3.zero)
{
// 计算玩家角色的移动速度
float moveSpeed = IsRunning() ? runSpeed : walkSpeed;
// 计算玩家角色的移动方向
Vector3 moveDirection = Vector3.RotateTowards(transform.forward, targetDirection, moveSpeed * Time.deltaTime, 0.0f);
moveDirection.y = 0.0f;
// 移动角色
transform.position += moveDirection * moveSpeed * Time.deltaTime;
// 旋转角色
Quaternion targetRotation = Quaternion.LookRotation(moveDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSmoothing);
}
return targetDirection;
}
```
这段代码中,我们首先根据玩家输入的移动方向计算出目标方向向量,并且判断目标方向是否为零。如果目标方向不为零,我们就进一步计算玩家角色的移动速度和移动方向,并且将角色向移动方向移动。然后我们将角色朝向插值到移动方向,最后返回目标方向向量。
需要注意的是,这段代码还依赖于其他函数和变量的实现,如果需要完整的理解和使用它,可能需要查看整个代码文件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)