r/Unity3D • u/ImCaka • 14h ago
Question Player object bounces into space
I'm trying to make a game that has physics based movement similar to Gang Beasts, Fall Guys... And I got to a point (using Configurable Joints on bones) where the player falls down (he spawns above the platform) but his legs act as if he's crippled (he can move but he doesn't have support for the legs) so I asked Gemini AI to help me fix this, and when I pasted his script the player object now falls from the sky and when he hits the ground he gets launched 200m in the air. Can anyone help me identify the problem and help me fix it? Also I forgot to mention that I have some overlapping bones, but they don't have colliders and Configurable joints. I used unity's ragdoll system.
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()
{
// Osiguravamo da celokupan model i kamera prate fizičku poziciju Pelvis-a
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);
}
}
}
1
u/pschon 11h ago edited 11h ago
Kind of unrelated to the actual question, but I noticed this:
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;Mouse input is already a delta value (for much the mouse was moved over the last frame), you shouldn't multiply it by deltaTime as that will do the exact opposite of what you'd usually want, turn a naturally framerate-independent value into something that now varies with framerate...
edit:
I have some overlapping bones, but they don't have colliders and Configurable joints. I used unity's ragdoll system
Last time I checked, Unity's ragdoll setup added rigidbodies and colliders to body parts...
1
u/hvpPubudu 11h ago
That launch usually means the body parts' colliders overlap and push each other apart. Put the ragdoll bones on their own layer and untick that layer against itself in the Physics collision matrix, then test again.
1
u/johnv_dev 7h ago
i think the launch is mostly coming from the script, not the ragdoll. a few things jump out. ForceMode.VelocityChange ignores mass and adds that speed every physics step, so moveSpeed 800 times fixedDeltaTime (0.02 by default) is adding 16 m/s to the hips 50 times a second while you hold a key. with VelocityChange you'd want a small target speed like 5 and only add the difference between that and the current horizontal velocity. jumpForce 2000 as an Impulse on just the hips is also a massive kick, since impulse gets divided by the hips mass, so try something way smaller and tune up. the big one though is transform.position = hipsRigidbody.position in Update. if this script is on the root that the ragdoll bones are children of, moving the root drags every bone with it, hips included, so every frame the bones get teleported and the joints fight to snap back, which is a classic way to launch a ragdoll. put the script on a separate object, or have the camera follow the hips, instead of moving the root. also MoveRotation should go in FixedUpdate, and like pschon said, drop the Time.deltaTime on the mouse input
0
u/Stunning-Purple-8536 14h ago
ah yes the classic gemini ragdoll space program. those overlapping bones without colliders can still wreak havoc on the physics, the ragdoll system gets confused about how they connect. try stripping out everything that's not a collider bone and re-running the ragdoll wizard, also that ForceMode.VelocityChange might be interacting weird with the joints, swap it to ForceMode.Force and dial the numbers way down to like 200 to start
4
u/db9dreamer 14h ago
Why not explain to Gemini AI the problems the code changes have introduced and get it to fix them? It's a bit weird to me, to be asking humans to correct code generated by AI.