- Home /
Manual Player Physics Help (C#)
I've written a good manual (not using Unity's default physics) movement physics script for my player with a character controller and everything works fine except for the fact that it doesn't react to outside forces. Meaning that the player can't walk through walls or go through the floor, but other objects can go inside of the player.
What is the best way to make moving objects displace the player?
Off the top of my head, when two things collide, they maintain total momentum, so figure out how much momentum you want to transfer to the player (use collision elasticity to solve for kinetic energy).
Not entirely sure what you mean. As of right now, I have this:
//Doesn't work
//"Velocity" is my artificial velocity
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody hitBody = hit.collider.attachedRigidbody;
if (hitBody == null || hitBody.is$$anonymous$$inematic)
return;
if (hit.moveDirection.y < -0.3F)
return;
Vector3 pushDir = new Vector3(hit.moveDirection.x, hit.moveDirection.y, hit.moveDirection.z);
Vector3 pushVel = new Vector3(hitBody.velocity.x, hitBody.velocity.y, hitBody.velocity.z);
hitBody.velocity += (pushDir * Velocity.magnitude)/hitBody.mass;
Velocity += -pushDir * new Vector3(pushVel.x, pushVel.y, pushVel.z).magnitude;
}
It definitely depends on what you want, but if you want realistic physics, you'll either have to study actual physics (hard) or switch to Unity's existing system (easy and probably more optimized).
Your answer
![](https://koobas.hobune.stream/wayback/20220612172833im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Non slippery movement 1 Answer
Gravity and rotation control 0 Answers
AddExplosionForce 0 Answers
Keeping player stationary and let physics affect... 0 Answers
Best way to physics collision 1 Answer