Search Unity

Garry's Mod Gravity Gun like script for Unity3d

Discussion in 'Scripting' started by Laumania, Feb 14, 2019.

  1. Laumania

    Laumania

    Joined:
    Jun 27, 2012
    Posts:
    109
    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 :(


    The closes I found it this script: https://pastebin.com/w1G8m3dH

    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.

    UPDATE UPDATE: Turned out there was a little more help to get over at Reddit in the Unity3d subreddit.
    https://www.reddit.com/r/Unity3D/comments/at17yd/help_to_improve_an_opensource_physics_gun_script/
     
    Last edited: Feb 22, 2019
  2. Zero_Xue

    Zero_Xue

    Joined:
    Apr 18, 2012
    Posts:
    116
    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
     
  3. Laumania

    Laumania

    Joined:
    Jun 27, 2012
    Posts:
    109
    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.
     
  4. Laumania

    Laumania

    Joined:
    Jun 27, 2012
    Posts:
    109
    Last edited: Feb 16, 2019
  5. Laumania

    Laumania

    Joined:
    Jun 27, 2012
    Posts:
    109
    ...damn I just realized that it's not really the "Gravity Gun" I'm after here....it's the PHYSICS GUN!

    I'm an idiot...anyway, the internet still seem to be empty of samples of how to build that functionallity in Unity3d.

    So any links to such a solution would still be awesome.
     
  6. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,585
    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.
     
  7. Laumania

    Laumania

    Joined:
    Jun 27, 2012
    Posts:
    109
    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.

    https://github.com/Laumania/Unity3d-PhysicsGun
     
  8. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    787
    You want the rotate the object together with the players rotation,right?

    So that the same exact point on the object that is facing you, will still be facing you, no matter how you rotate your view/camera as the player.

    In that case the solution is really simple.
    Just apply the same rotation the player does, to the object you are holding.

    Here is how you do it, I'm typing this from my phone tho so the code will be a bit messy, sorry for that :p


    1. Find your own rotation delta:
    Var rd = Quaternion.FromTo(playerRotationLastFrame, playerRotationCurrentFrame);

    2. Apply that to the object:

    holdingObject.transform.rotation = rd * holdingObject.transform.rotation;


    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
     
  9. Laumania

    Laumania

    Joined:
    Jun 27, 2012
    Posts:
    109
    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 :)

    https://github.com/Laumania/Unity3d-PhysicsGun
     
  10. Laumania

    Laumania

    Joined:
    Jun 27, 2012
    Posts:
    109
    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.

    Anyway, as mentioned take a look here if you have time: https://github.com/Laumania/Unity3d-PhysicsGun

    I'll continue and see how far I can take this :)
     
  11. Laumania

    Laumania

    Joined:
    Jun 27, 2012
    Posts:
    109
    Ok, I made a video now explaining why I need this physics gun and the current problems I have with it.

    #unitydevs please help me these objects rotate properly :) See the last 5 min of the video where I show the current issues:



    Thanks in advance.