Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Hold object Script.

Discussion in 'Scripting' started by MadameToasty, Sep 23, 2011.

  1. MadameToasty

    MadameToasty

    Joined:
    Sep 22, 2011
    Posts:
    4
    Does anyone know a script which allows you to hold the left mouse button to hold an object with a rigid body in air. Pretty much like in Portal. I'm a complete newbie when it comes to scripting so all help is appreciated! :)
     
  2. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    To ask other members for complete scripting solutions is unfortunately not the way it works. And that`s what you are asking for here. Divide your problem into its solvable subparts. And then solve it one by one. A first step could be to write a script where you can change a variable by clicking with the mouse.
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Well, that was a wonderful reply.

    First, get if you have a mouse button down:

    http://unity3d.com/support/documentation/ScriptReference/Input.GetMouseButton.html

    A rigid body can be held in the air by setting it's Position and Velocity. (Set the velocity to zero)

    http://unity3d.com/support/documentation/ScriptReference/Transform-position.html
    http://unity3d.com/support/documentation/ScriptReference/Rigidbody-velocity.html

    You can calculate the position that you want by using the forward vector of the camera if you are in first person.

    http://unity3d.com/support/documentation/ScriptReference/Camera-main.html
    http://unity3d.com/support/documentation/ScriptReference/Transform-forward.html

    This will be a normalized vector facing forward (normalized means it has a length of 1, so you can multiply times a distance and get a correct distance to the place you want it to be.)

    Go forth and play, come up with some script and post back if you have questions. :D
     
  4. kaarme

    kaarme

    Joined:
    May 1, 2014
    Posts:
    177
    It still falls when velocity is set to zero.
     
  5. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    turn off gravity while it is held
     
  6. exvalid

    exvalid

    Joined:
    Oct 20, 2017
    Posts:
    9
    Hey I have added 2 Script to Complete a full Grab toggle and ready to move code,

    Fixed An Inversion Error and a Euler read error while moving. since yesterday it is now working as stated below

    ObjectReplyIdAndLock, ObjectGrabIdAndMove

    ObjectGrabIdAndLock goes on Main player,.. ObjectReplyIdAndMove goes on Moveable Objects remember to add layers in you want to hit in inspector and pick the Headcam Ect

    Updated Scripts Since the other week now with full movement And Rotation and Full Inversion Options

    This is Complete bar Diagnals and Mouse Rotation as im adding this now, you want to use the option OverideDiagnals and possibly UseDefaultRotation if u hate my defualt.

    too add mouse copy the whole auto inversion and paste it underneath and swap the names to the mouse names instead of the default keys its a mission dont attempt it lol. i will do this over the week as still cleaing up the script its pretty large, Please contact at Exvalid@gmail.com To give me job coding.

    cheers Ryan kappeslink text Exvalid@gmail.com
     

    Attached Files: