Ok, I have search the web but really can’t find anything good on this.
I need a script the works similar to the physics gun of Garry’s Mod, not with graphics, laser rays and stuff, just the core “grabbing, throwing around, rotation, freezing” mechanics.
Like here, sadly this guy apparently don’t want to share any thing except the video
This kind-of-works, but have some issues, mainly that the rotation of the object you hold, doesn’t stick with a relative rotation to the player when he turns around.
Anyway, I can show and go more into details with my specific issue, but that might be another thread, my question here is - does anybody have/know about a script that mimics the physics gun from Garry’s mod?
UPDATE: I just realized that it’s not the Gravity Gun I’m actually after…it’s the PHYSICS GUN mechanics. Updated the post content to fit that, sadly I cannot edit the title.
I would look into Physics.Linecast to get the object your pointing at and store it then have that object follow your mouse cursor with Camera.main.ScreenToWorldPoint(Input.mousePosition) - (possibly the wrong command) - ,as for rotating the object if you have the object stored you can mess about with its transform
Thanks for your reply, but I actually already got most of it working, but have some rotation issues and was just thinking that I couldn’t be the first to do this in Unity3d
I need this for my fireworks game I’m working on…but I’m thinking about doing an open-source project on Github, just to have this specific gravity gun-like mechanism.
Then others could benefit and contribute.
I’d start breaking that down into smaller problems. For instance, the first thing I’d look at is “picking up” objects that have rigidbodies, and “holding” them in front of the players view. This could be done different ways, but one way is a linecast/raycast from the point of view forwards, and if an object is found, set its rigidbody kinematic, and move it to a certain position in front of the camera (maybe have an empty object that is a child of the camera, where you would like objects to float when “held”, and use its position to move the object around).
Once you can “pick it up” and hold it, move onto the next issue from there.
That’s exactly how I have done so far, but are facing annoying rotation issues and thought that somebody might already have created a script like this.
I have isolated this part of my game in below github repo, for easier to get help from others, to easier play around with it and so others can benefit from the solution I end up with.
That should do the trick.
Now the object will be “rotationally locked” relative to you.
If you’d be holding a cube where each face has a different color, and you only see the blue one currently, then with this code you will always see the blue one, no matter how you rotate.
Edit: in case that’s unclear
“playerRotationCurrentFrame” is simply
player.transform.rotation
Thanks for your answer. I’m working on it here. As stated I have isolated this particular issue from my game, so people can help out and others in the future can reuse the script in their project.
So if you have time, please help out and do a Pull Request - I’ll keep work on it myself
I might forget to mention that I’m working with rigidbodies here, so I don’t want to set a rotation directly, as it will conflict with the physics engine.