Files
MuHua-Core/Packages/Character/Runtime/Player/PlayerController.cs
T
2025-03-10 23:44:06 +08:00

43 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace MuHua {
public class PlayerController : MonoBehaviour {
public Transform cameraController; // 相机对象
public PlayerCharacter character;
private Vector2 moveInput;
private void Update() {
if (moveInput == Vector2.zero) { return; }
// 获取相机的前向和右向
Vector3 cameraForward = cameraController.transform.forward;
Vector3 cameraRight = cameraController.transform.right;
// 忽略相机的y轴
cameraForward.y = 0;
cameraRight.y = 0;
// 归一化向量
cameraForward.Normalize();
cameraRight.Normalize();
// 计算相对于相机的移动方向
Vector3 moveDirection = (cameraForward * moveInput.y + cameraRight * moveInput.x).normalized;
// 相对于玩家的移动方向
Vector3 position = character.transform.position + new Vector3(moveDirection.x, 0, moveDirection.z) * 0.5f;
PlayerKinesisMove kinesis = new PlayerKinesisMove(position, character);
character.Updatekinesis(kinesis);
}
#region
public void OnMove(InputValue inputValue) {
// 获取移动输入
moveInput = inputValue.Get<Vector2>();
}
#endregion
}
}