I’m working on a game in which a player avatar navigates a maze. The problem is that when the avatar collides with a wall, it bounces off and falls over. How can I modify the script so that when a collision occurs, the avatar stops at the wall but remains upright?
are you using a FPS component with capsule collision for the player control ? those don’t fall over
I think you can set the avatar rigidbody to isKinematic = true. That should keep him upright…unless you need a non-kinematic avatar for some reason.
The problem is that when you set the avatar to isKinematic, it suddenly becomes able to walk through walls. I want it to stop at the wall, not walk through or fall over.
There is a little trick you can use to keep a physics-controlled object upright after a collision. You can use rigidbody.AddForceAtPosition to apply a force at two different points on the object, one slightly above the centre and the other slightly below:-
var rightingForce = Vector3.up * 10.0;
rigidbody.AddForceAtPosition(rightingForce, transform.TransformPoint(Vector3.up));
rigidbody.AddForceAtPosition(-rightingForce, transform.TransformPoint(-Vector3.up));
The two forces just cancel each other out when the object is upright. When it leans, the top force is no longer directly above the bottom force, so the object twists to get them back in alignment.
…or just force the “up” vector of the orientation to be straight up, and re-orthonormalize the orientation matrix
OK, I tried andeeee’s suggestion and now the avatar remains upright, but it still bounces away from the wall, which I don’t want it to do. I don’t really care about having realistic physics, I just want the avatar to understand that the walls are there and it can’t go through them.
make sure you didnt set the material property of the collider to “bouncy” or “rubber”
it shouldn’t bounce off if its set to “metal” or “wood”
You could try setting the wall’s to “ice” so collisions slide along…
andee’s suggestion, might also be using too much force, and is bouncing you away from the wall, try reducing the force, Or try the Orthonormalizing trick.
Both materials have been set to “wood” the entire time. Is there some way to signal to the object that the wall is there while it’s set to isKinematic? Maybe then I could just have the script tell it not to go through the wall.
Just use a character controller component like in the Unity Island demo.
everything you need is built into unity, to make a character move and collide with objects without falling over.
Checkout the “First Person Controller Prefab” in the unity island demo.
If you need 3rd person camera controls… rip out the first person camera controls, and put in your own 3rd person camera.