moving a rigidbody object

I want to pick up a rigidbody block with an onclick function and be able to move it around my scene. So if you click and hold, you carry the block, but if you stop holding down left click, the rigidbody block falls. Can anyone help please?

4 Answers

4

There is a standard package script in Standard Assets/Scripts called DragRigidbody that should do what you need.

Ok I didnt realize that was there, however I need to edit and add some stuff to it, so if anyone can help that would be great. I to add a set distance of a maximum of 5 away from the object. Also I need to make it so if the player is touching the object, they can't click to control it. I'm using a crate, so that keeps the player from standing on the crate and flying around the room.

If you look carefully at the Drag Rigidbody Script, it has several variables that you can alter in the Property inspector after you attach it to an object( with a rigid body of course)
Those Variables are

  • var spring
  • var damper
  • var drag
  • var angularDrag
  • var distance

Var Distance is the one you most likely want…but if you play around with it you can see what each effect does…and you could add a function that rotates the object to align with the camera vector, if you want it to align with your view …like Halflife2 or Deus Ex does… The rest of the tweaking could be done with spring and damping

0.2;

var attachToCenterOfMass = false;

If you look carefully at that Drag Rigidbody Script, it has several variables that you can alter in the Property inspector after you attach it to an object( with a rigid body of course)
Those Variables are

  • var spring
  • var damper
  • var drag
  • var angularDrag
  • var distance
  • var attachToCenterOfMass

Var distance is the one you most likely want…but if you play around with it you can see what each effect does…and you could add a function that rotates the object to align with the camera vector, if you want it to align with your view …like Halflife2 or Deus Ex does… The rest of the tweaking could be done with spring and damping

Hope that helps
if the object you are picking up is having issues with other colliders you could turn the objects physics component off while you are carrying it …but then it would most likely poke into other objects visually …
Another way to stop the flying effect you mentioned would be to add a world space coordinates check of the player, and disable the pickup script functions if they read true.
ie: if you are standing right on the area that the object occupies in world space( usually its collision mesh world position)