我正在尝试制作一款类似《Gang Beasts》《Fall Guys》的物理驱动移动游戏。我遇到的问题是(在骨骼上使用 Configurable Joint)玩家从平台上方 spawn 后会掉落,但他的腿像瘸腿一样(可以移动但没有腿支撑),于是我向 Gemini AI 求助,粘贴他的脚本后,玩家物体现在从天空坠落,撞地后会被弹射 200 米高空。请问有谁能帮我找出问题并帮我修复?另外,我忘记提到我有些骨骼重叠,但它们没有 colliders 和 Configurable Joint,我使用了 Unity 的 ragdoll 系统。
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("Physics & Movement")]
public Rigidbody hipsRigidbody;
public float moveSpeed = 800f;
public float jumpForce = 2000f;
[Header("Camera & Look")]
public Transform cameraTransform;
public float mouseSensitivity = 200f;
public float minPitch = -85f;
public float maxPitch = 85f;
[Header("Ground Check")]
public Transform groundCheck;
public float groundDistance = 0.8f;
public LayerMask groundMask;
private float pitch = 0f;
private float yaw = 0f;
private bool isGrounded;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
if (hipsRigidbody != null)
{
hipsRigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
}
}
void Update()
{
// 确保整个模型和摄像机跟随骨盆的实际位置
if (hipsRigidbody != null)
{
transform.position = hipsRigidbody.position;
}
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
yaw += mouseX;
pitch -= mouseY;
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
if (cameraTransform != null)
{
cameraTransform.localRotation = Quaternion.Euler(pitch, yaw, 0f);
}
if (hipsRigidbody != null)
{
hipsRigidbody.MoveRotation(Quaternion.Euler(0f, yaw, 0f));
}
if (groundCheck != null)
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
hipsRigidbody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
void FixedUpdate()
{
if (hipsRigidbody == null) return;
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 moveDir = (transform.right * x + transform.forward * z).normalized;
if (moveDir.magnitude > 0.1f)
{
Vector3 targetVelocity = moveDir * (moveSpeed * Time.fixedDeltaTime);
hipsRigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);
}
}
}
评论 (0)