Unity3D 为什么我的人物加了rigbody和胶囊碰撞体,墙加了盒子碰撞体,为什么人物靠近墙就会被吸附过去,然后就无法行走位移,但是动画啥的都还能播放。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("移动参数")]
[SerializeField] float moveSpeed = 10.0f;
[SerializeField] float runSpeed = 15.0f;
[SerializeField] float jumpForce = 5.0f;
[SerializeField] bool isGrounded = true;
[Header("相机设置")]
[SerializeField] private List<Camera> cameras = new List<Camera>();
private bool isRunning = false;
private Vector3 moveDir;
private Camera playerCamera;
private Rigidbody rb;
private Animator ani;
private int currentCameraIndex = 0;
// 添加一个变量来跟踪是否有输入
private bool hasInput = false;
void Start()
{
rb = GetComponent<Rigidbody>();
ani = GetComponent<Animator>();
// 确保刚体没有位置冻结
rb.constraints = RigidbodyConstraints.FreezeRotation;
// 初始化所有动画状态为Idle
ResetAnimationStates();
// 初始化相机
InitializeCameras();
//Debug.Log("玩家初始化完成,初始状态应为Idle");
}
void Update()
{
HandleInput();
HandleCameraSwitch();
UpdateAnimation();
}
void FixedUpdate()
{
Move();
}
void InitializeCameras()
{
// 激活第一个相机,禁用其他相机
for (int i = 0; i < cameras.Count; i++)
{
if (cameras[i] != null)
{
cameras[i].gameObject.SetActive(i == 0);
BaseCamera baseCamera = cameras[i].GetComponent<BaseCamera>();
if (baseCamera != null)
{
baseCamera.SetPlayerTransform(transform);
}
}
}
// 设置当前使用的相机
if (cameras.Count > 0 && cameras[0] != null)
{
playerCamera = cameras[0];
}
}
void HandleCameraSwitch()
{
if (Input.GetKeyDown(KeyCode.Tab) && cameras.Count > 1)
{
// 禁用当前相机
if (cameras[currentCameraIndex] != null)
{
cameras[currentCameraIndex].gameObject.SetActive(false);
}
// 切换到下一个相机
currentCameraIndex = (currentCameraIndex + 1) % cameras.Count;
// 激活新相机
if (cameras[currentCameraIndex] != null)
{
cameras[currentCameraIndex].gameObject.SetActive(true);
playerCamera = cameras[currentCameraIndex];
}
Debug.Log($"切换到相机: {currentCameraIndex + 1}");
}
}
void HandleInput()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// 检查是否有有效输入 - 使用更严格的阈值
hasInput = (Mathf.Abs(horizontal) > 0.01f || Mathf.Abs(vertical) > 0.01f);
if (playerCamera != null)
{
Vector3 cameraForward = playerCamera.transform.forward;
Vector3 cameraRight = playerCamera.transform.right;
cameraForward.y = 0;
cameraRight.y = 0;
cameraForward.Normalize();
cameraRight.Normalize();
moveDir = (cameraForward * vertical + cameraRight * horizontal).normalized;
}
else
{
// 如果没有相机,使用世界坐标方向
moveDir = new Vector3(horizontal, 0, vertical).normalized;
}
// 检测R键按下切换跑步状态
if (Input.GetKeyDown(KeyCode.R))
{
isRunning = !isRunning;
Debug.Log($"跑步状态切换: {isRunning}");
}
}
void Move()
{
if (moveDir.magnitude >= 0.1f && hasInput)
{
float currentSpeed = isRunning ? runSpeed : moveSpeed;
Vector3 movement = moveDir * currentSpeed * Time.fixedDeltaTime;
rb.MovePosition(transform.position + movement);
}
}
void UpdateAnimation()
{
// 调试信息
//if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D) ||
// Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.RightArrow))
//{
// Debug.Log($"按键按下 - 输入状态: {hasInput}, 跑步状态: {isRunning}");
//}
// 根据输入状态和跑步状态更新动画
if (hasInput)
{
if (isRunning)
{
// 跑步状态
SetAnimationState(false, true, false);
Debug.Log("切换到跑步状态");
}
else
{
// 行走状态
SetAnimationState(true, false, false);
Debug.Log("切换到行走状态");
}
}
else
{
// 空闲状态
SetAnimationState(false, false, true);
}
}
void SetAnimationState(bool walking, bool running, bool idle)
{
ani.SetBool("isWalking", walking);
ani.SetBool("isRunning", running);
ani.SetBool("isIdle", idle);
}
void ResetAnimationStates()
{
// 重置所有动画状态,确保初始状态为Idle
ani.SetBool("isWalking", false);
ani.SetBool("isRunning", false);
ani.SetBool("isIdle", true);
// 强制Animator立即更新状态
ani.Update(0f);
Debug.Log("动画状态已重置为Idle");
}
}