- Home /
How do I add gravity to an object?
Hi there, I have got my hands on a script which allows me to pick up objects and move them...
private var pickObj: Transform = null;
private var hit: RaycastHit;
private var dist: float;
private var newPos: Vector3;
function Update(){
if (Input.GetMouseButton(0)){ // if left button creates a ray from the mouse
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!pickObj){ // if nothing picked yet...
if (Physics.Raycast(ray, hit) && hit.transform.tag == "Pick"){
// if it's a rigidbody, zero its physics velocity
if (hit.rigidbody) hit.rigidbody.velocity = Vector3.zero;
pickObj = hit.transform; // now there's an object picked
// remember its distance from the camera
dist = Vector3.Distance(pickObj.position, Camera.main.transform.position);
}
}
else { // if object already picked...
newPos = ray.GetPoint(dist); // transport the object
pickObj.position = newPos; // to the mouse position
}
}
else { // when button released free pickObj
pickObj = null;
}
}
But I want to add gravity to the objects so that when I let go or press the button again the object drops to the floor / Ground!? Thanks -Izzy
Answer by Seth-Bergman · Aug 13, 2012 at 12:57 AM
did you try just checking "use gravity" on the rigidbody? since your script is accessing the transform directly anyway in the update, that may be all you need (unless you need the object to ignore physics...)
Thanks! I forgot about the RigidBody D: Works Perfectly now :D
Thanks Again!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613075554im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Drag And drop objects mouse 2 Answers
Moving object along its own axis 2 Answers
How do I make an object move? 3 Answers
I am trying to allow the player to pick up and move objects in a FPS style game 0 Answers