- Home /
How to stop an object from moving rigidbody
In my game I have a FirstPersonControllerPrefab which is parent to a pickaxe which I have animated for both active and idle states. I want my pickaxe to collide with the rocks I am mining, but when it does, it pushes the rocks out of the way as I move forward.
My FPController has a rigidbody attached and my pickaxe has a kinematic rigidbody. There is a sphere collider at the tip of my pickaxe which is where the rocks are pushed from. If I make this collider a trigger, the rocks aren't pushed but my pickaxe goes throuh the surface which is not what I want.
Is there a way I can have the pickaxe collide with another rigidbody whilst being unable to move?
Any help would be greatly appreciated,
Francis.
PS: I have tried freezing the movement of the rocks from the rigidbody but this just means that the pickaxe goes through the rock even if it isn't a trigger. I have also tried setting the mass of the rocks to 999999 but this has no effect.
Answer by syclamoth · Nov 08, 2011 at 05:53 AM
There are a couple of ways, here- you can use Layer-based collision to stop the rocks from colliding with anything except the ground and the pickaxe, and you could set up the collider on the pick so that it acts like a trigger whenever you aren't swinging it.
I had a go with both of these methods but neither worked. The problem is that because the pickaxe is parented to the FPController, when the player moves, the pickaxe does the exact same thing. This means that even if I do have the rock set to only collide with ground and pickaxe, moving forward will still push the pickaxe into the rock and move it. The collider at the end of the pickaxe is the only thing affecting the rock, if I turn my back to the rock and try to push without the contact of pickaxe to rock, I collide, but do not move it. If I remove the pickaxe's collider completely, it passes through and the controller collides with the rock without moving.
Let me get this straight, you want to have the rock act as a rigidbody when you hit it with the pick, but act like a static collider for your character?
$$anonymous$$ore or less yes... Everything is fine with the setup I have at the moment EXCEPT when the pickaxe's collider touches the rock it pushes it out of the way ins$$anonymous$$d of just hitting it like a pickaxe in real life would do. Sorry if i'm confusing you, I've only just started making games with unity
No, no that's fine. Well, for this I'd just have the pickaxes' collider set to
isTrigger = true;
while it's not actually swinging, that way it won't push things out of the way when you don't want it to.
It's a good idea but it still leaves the problem of the pickaxe going through the rock when the player isn't picking which isn't very realistic. What I have done is ins$$anonymous$$d placed a virtual copy of the pickaxe on it's own layer which removes this problem, and i'll make an invisible trigger collider where the pick would of been to handle the collisions :) http://answers.unity3d.com/questions/17841/always-render-a-object-on-top-of-another.html
Thanks for all your help though!