- Home /
How to make Rigidbody detect collisions but stop its movements
Hello guys, i'm making a third person controller using Translate for most of the movements, I use raycasts to detect both the lower and upper collisions, but detecting the collisions on x and z axis would demand a lot of raycasts, and i think that would be a bit heavy for the memory to handle. Then i decided to use a rigidbody, lock its Y position ( because i handle the Y position with the raycasts and all), and removed the gravity, because i already have a custom gravity with translate that respect the collisions because of the raycasts. because I wasn't using gravity, whenever i hitted a wall, the character keeped going the other way, but then i solved it by making: rigidbody.velocity = Vector3.zero; every frame, it stopped whatever velocity it gained and the charater stopped going through walls. But even so, having the rigidbody gave me some strange results, whenever i jump, the character starts shaking up and down ( while respecting the overall "route" of the jump ), and this is odd, because the Y position of the rigidbody is locked... And sometimes the character shakes with no apparent reaseon, I think its caused because the rigidbody sometimes gets a velocity in some axis, and even with my "rigidbody.velocity = Vector3.zero", i can't stop it completely. Well, what i want is to make my rigidbody works like a kinematic ( when i check the kinematic button, all shaking stops ) but still stop going throught walls... i know its kind of a paradox, but i guess you guys get it.
If its not possible, someone knows a simple way to detect collisions on x and z axis with raycasts? ( I think I could get the vector resulting from the x and z movement that my character does while walking, make a small raycast in this direction, and if it hits a wall, my character stops moving.. but i guess it wouldn't be enough... )
Answer by TheMidn1ght · Dec 03, 2013 at 12:39 AM
The problem is that you are using Translate to move your object which does not take into account anything physics related. This is probably why you were seeing odd things with the RigidBody. You will need to manually do the physics checks on your own with Raycast like you are thinking or you can convert all your movement code to utilizing the functionality given to you by the Physics engine in Unity by editing variables on your RigidBody and moving him with the velocity variable you have already tinkered with instead of just flat moving him with translate. Adding force and velocity and stuff like that will give you all the Physics collision based things for free.
Thanks for your answer :) I'm using translate because I'm making a plataformer, and i need total control of the movements and the physics. I tried to do the same with rigidbodies once, but I couldn't get it to be so precise. Don't you a way to detect the walls with raycast and prevent the character from going through walls?