- Home /
Physics gun like in Garry's Mod?
Like in this video https://www.youtube.com/watch?v=tx2mlbFu420
It looks like it works quite well, and actually, I could make it too. But I noticed that his rigidbody position stays relative to the point where you pick up the object. How could I make it do that?
I guess handling rotation would be pretty simple. Make it detect keypress. Pause mouselook. Translate mouse axis movement to object rotation. Done.
Answer by Laumania · Mar 01, 2019 at 03:46 PM
All you need you will find here :)
https://www.youtube.com/watch?v=ZnWfChctC9M
I had a need for a Physics Gun too - but couldn't find it. So made one with help from the community (mainly a guy called WarmedxMints) :)
Answer by mhernandez88 · Jul 21, 2016 at 04:27 PM
This post is old, but i thought I would answer it in theory.
The theory is you get the raycast target, and access it public attributes such that you can enable "isKinematic". Then with the raycast you can begin to attach the objects transform to the players look position. When you disable the gun, you disable isKinematic and the object will fall.
If you wish to handle rotations you just do that in the same way. if ( mutatingObject ) if ( button.ctrl ) vec3 = new Vector3( input.horizontal, input.vertical .... blah ) target.rotation = vec3
Actually no. The OP of the video stated on a reddit post:
" Nah, the rigidbody is enabled at all times. I needed the object to remain affected by physics while it was being held. If I were to simply parent the prop to a Transform then I would lose physics and it would start clipping through the map etc.
Setting it to kinematic as /u/$$anonymous$$easuring mentioned wouldn't work as kinematic bodies don't have velocity and are unaffected by physics.
$$anonymous$$y solution was to disable the prop's gravity upon grabbing. It is moved around via velocity and thus remains as a fully physics intractable object. This gives me all the benefits of interacting with the physics engine in a fluid way (Collisions are normal, no addition calculation needed after throwing/releasing as it has velocity, etc) "
Answer by jamezzz · Aug 12, 2018 at 01:25 PM
Dead thread time:
as @HyblockerDevelopment said, keeping the rigidbody enabled whilst moving the object is critical to ensure you can't drag objects through walls. In theory you could have an invisible gameobject (I WILL CALL THIS TARGET)at a fixed distance from the physGun at all times acting as a transform. Then when you pick up an object (using raycast to get the object as a reference), you can use Physics.AddForce() in the direction of the TARGET to move the gameobject towards where the physGun is looking but retaining collisions of the object with walls.
to use the scroll wheel to "push" and "pull" the object away from the player as you can do in garry's mod, simply move the target gameobject away from the player on the movement of the scrollwheel.
sorry, again this is an old thread but it's fun to answer.