add
This commit is contained in:
parent
3b5b8b8365
commit
2b5f9b33c5
@ -5831,7 +5831,15 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
joystick: {fileID: 172478311}
|
||||
speed: 5
|
||||
cameraTransform: {fileID: 1068997307}
|
||||
moveSpeed: 3
|
||||
characterControl: {fileID: 0}
|
||||
mainCamera: {fileID: 1068997306}
|
||||
normalFOV: 60
|
||||
sprintFOV: 120
|
||||
fovChangeSpeed: 2
|
||||
walkTime: 1.5
|
||||
runTime: 1.3
|
||||
--- !u!1 &1242008297200351719 stripped
|
||||
GameObject:
|
||||
m_CorrespondingSourceObject: {fileID: 5795828957522809712, guid: 2c4a835a9676e1a478c77d64f472c3ec, type: 3}
|
||||
@ -6033,6 +6041,14 @@ PrefabInstance:
|
||||
propertyPath: m_Materials.Array.data[0]
|
||||
value:
|
||||
objectReference: {fileID: 2100000, guid: 6553b7fd95798f44f9d009019a9c516f, type: 2}
|
||||
- target: {fileID: 4527286627946239905, guid: 006066e2380528243a52cbfc56f6d189, type: 3}
|
||||
propertyPath: m_UseGravity
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 4527286627946239905, guid: 006066e2380528243a52cbfc56f6d189, type: 3}
|
||||
propertyPath: m_IsKinematic
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 4527286627946239909, guid: 006066e2380528243a52cbfc56f6d189, type: 3}
|
||||
propertyPath: mainCamera
|
||||
value:
|
||||
|
@ -63,9 +63,15 @@ public class CameraControl : MonoBehaviour
|
||||
{
|
||||
// 获取鼠标移动来控制相机的旋转。
|
||||
// 鼠标控制:
|
||||
angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1, 1) * horizontalAimingSpeed;
|
||||
angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1, 1) * verticalAimingSpeed;
|
||||
|
||||
|
||||
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1, 1) * horizontalAimingSpeed;
|
||||
angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1, 1) * verticalAimingSpeed;
|
||||
}
|
||||
|
||||
|
||||
// 设置垂直移动的限制。
|
||||
angleV = Mathf.Clamp(angleV, minVerticalAngle, targetMaxVerticalAngle);
|
||||
|
||||
|
@ -29,7 +29,6 @@ public class CharacterControl : MonoBehaviour
|
||||
//垂直和水平输入值计算出一个方向向量
|
||||
Vector3 dir;
|
||||
|
||||
private PlayerMovement_Jpystick PlayerMovement_Jpystick;
|
||||
|
||||
|
||||
//判断是否在移动
|
||||
@ -67,7 +66,7 @@ public class CharacterControl : MonoBehaviour
|
||||
void Start()
|
||||
{
|
||||
|
||||
PlayerMovement_Jpystick = GetComponent<PlayerMovement_Jpystick>();
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
@ -116,8 +115,8 @@ public class CharacterControl : MonoBehaviour
|
||||
vertical = Input.GetAxis("Vertical");
|
||||
horizontal = Input.GetAxis("Horizontal");
|
||||
|
||||
vertical = PlayerMovement_Jpystick.joystick.Vertical;
|
||||
horizontal = PlayerMovement_Jpystick.joystick.Horizontal;
|
||||
//vertical = PlayerMovement_Jpystick.joystick.Vertical;
|
||||
//horizontal = PlayerMovement_Jpystick.joystick.Horizontal;
|
||||
|
||||
// 使用摄像头的方向来计算角色的移动方向
|
||||
Vector3 forward = mainCamera.transform.forward; // 摄像头前方
|
||||
@ -161,7 +160,7 @@ public class CharacterControl : MonoBehaviour
|
||||
}
|
||||
|
||||
//移动状态
|
||||
void MoveState()
|
||||
public void MoveState()
|
||||
{
|
||||
if (MoveTime < walkTime) // 慢走
|
||||
{
|
||||
|
@ -4,15 +4,108 @@ using UnityEngine;
|
||||
|
||||
public class PlayerMovement_Jpystick : MonoBehaviour
|
||||
{
|
||||
public Joystick joystick; // ½«JoystickÍÏÈë´Ë´¦
|
||||
public float speed = 5f;
|
||||
public FixedJoystick joystick; // 引用 Fixed Joystick
|
||||
public Transform cameraTransform; // 引用主摄像机的 Transform
|
||||
public float moveSpeed = 5f;
|
||||
|
||||
public CharacterControl characterControl;
|
||||
private CharacterAin Characterain;
|
||||
|
||||
public Camera mainCamera;
|
||||
|
||||
private Rigidbody rb;
|
||||
// 定义相机 FOV 相关变量
|
||||
public float normalFOV = 60f; // 正常行走时的FOV
|
||||
public float sprintFOV = 120f; // 奔跑时的FOV
|
||||
public float fovChangeSpeed = 2f; // FOV改变的速
|
||||
|
||||
private bool IsMoving = false;
|
||||
//跑步切换的时间
|
||||
private float MoveTime = 0f;
|
||||
//走路的时间
|
||||
public float walkTime;
|
||||
//跑的时间
|
||||
public float runTime;
|
||||
//跑步时平滑切换fov
|
||||
private float targetFOV;
|
||||
private void Start()
|
||||
{
|
||||
characterControl = GetComponent<CharacterControl>();
|
||||
Characterain = GetComponent<CharacterAin>();
|
||||
rb = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
// 1. 获取摇杆输入
|
||||
float horizontal = joystick.Horizontal;
|
||||
float vertical = joystick.Vertical;
|
||||
|
||||
Vector3 direction = new Vector3(horizontal, 0, vertical);
|
||||
transform.Translate(direction * speed * Time.deltaTime, Space.World);
|
||||
// 2. 转换为三维方向向量 (保持在水平面)
|
||||
Vector3 inputDirection = new Vector3(horizontal, 0f, vertical);
|
||||
|
||||
// 3. 获取移动方向相对于摄像机的世界方向
|
||||
Vector3 cameraForward = cameraTransform.forward; // 摄像机的前向方向
|
||||
Vector3 cameraRight = cameraTransform.right; // 摄像机的右方向
|
||||
|
||||
// 由于我们只需要水平的移动方向,去掉摄像头的y轴分量
|
||||
cameraForward.y = 0;
|
||||
cameraRight.y = 0;
|
||||
|
||||
cameraForward.Normalize();
|
||||
cameraRight.Normalize();
|
||||
|
||||
// 4. 计算最终的移动方向 (相对于摄像机的前后左右)
|
||||
Vector3 moveDirection = (cameraRight * horizontal + cameraForward * vertical).normalized;
|
||||
|
||||
|
||||
if (Mathf.Abs(vertical) > 0.01f || Mathf.Abs(horizontal) > 0.01f)
|
||||
{
|
||||
if (MoveTime < (walkTime + runTime + 0.1f))
|
||||
{
|
||||
MoveTime += Time.deltaTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 5. 应用移动
|
||||
if (moveDirection.magnitude > 0.1f)
|
||||
{
|
||||
// 使用速度移动角色
|
||||
Vector3 newPosition = rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime;
|
||||
rb.MovePosition(newPosition);
|
||||
MoveState();
|
||||
// 使角色面朝移动方向
|
||||
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
|
||||
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 720 * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveState()
|
||||
{
|
||||
if (MoveTime < walkTime) // 慢走
|
||||
{
|
||||
Characterain.SetPlayerState(CharacterState.SlowWalk);
|
||||
targetFOV = normalFOV; // 设置目标FOV为正常值
|
||||
}
|
||||
else if (MoveTime < (walkTime + runTime)) // 普通行走
|
||||
{
|
||||
Characterain.SetPlayerState(CharacterState.walk);
|
||||
targetFOV = normalFOV + 30f; // 设置目标FOV为增加值
|
||||
}
|
||||
else // 奔跑
|
||||
{
|
||||
Characterain.SetPlayerState(CharacterState.run);
|
||||
targetFOV = sprintFOV; // 设置目标FOV为奔跑值
|
||||
}
|
||||
|
||||
// 使用插值平滑调整相机的FOV
|
||||
mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, targetFOV, fovChangeSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user