- Home /
Throwing an object (applying force to it when mouse is released)
I would like to be able to pick up, drag along, and finally throw an object.
this is my code so far: what it does so far is pick up the GameObject (the one i used in my tests is a cube named DraggableBox, located above a plane, with the Tag "draggable") drag it along my mouse, and then drop it again once the mouse is released. Now what i am trying to accomplish now (and so far haven't succeeded in) is to throw the object if the mouse is moving at the moment the mouse button is released, instead of just drop it. Much like the game Black and White 2.
Thanks in advance for any help you can give me with this.
var grabbed : Transform;
var grabDistance : float = 10.0f;
var moved : boolean = false;
var DraggableObject : GameObject;
var goTo : Vector3;
var targetPosition:Vector3;
var startPosition:Vector3;
var direction: Vector3;
function Update () {
if (Input.GetMouseButton(0)) {
if (grabbed){
Drag();
}
else{
Grab();
}
}
else{
grabbed = null;
}
if(Input.GetMouseButtonUp(0) && grabbed != null){
Throw();
}
}
function Grab() {
if (grabbed){
grabbed = null;
}
else {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit) && hit.collider.gameObject == GameObject.FindGameObjectWithTag("Draggable")){
startPosition= Input.mousePosition;
grabbed = hit.transform;
moved = true;
DraggableObject = hit.collider.gameObject;
}
}
}
function Throw() {
if (grabbed && moved==true){
targetPosition=Input.mousePosition;
direction = (targetPosition - startPosition);
direction.Normalize();
rigidbody.AddForce(direction * 15f, ForceMode.Impulse);
moved = false;
}
}
function Drag() {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
grabbed.position = ray.origin + ray.direction * grabDistance;
}
If you're trying to create something like the gravity gun from Half Life, here's and idea of how i'd try to implement:
Cast a ray from the camera to the mouse position
Create an empty GameObject (lets call it destination) in the collision point
$$anonymous$$ove the destination GameObject to the camera's hierarchy
Add into the collided object a script that will, essencially, apply a force (relative to distance) towards the destination GameObject's direction
When the user clicks, remove that script and aplly a force to the desired direction (away from the camera?)
that's not what i am trying to create here lol, did you read what i typed? i said like black and white, meaning you pick up and object, lets say you hold it in your hand for the time being. and then you move your mouse, let go off the button and then it flies away, as if you would have thrown it. not a gravity gun at all
How far did you get with this? I am also making a Black & White style game
$$anonymous$$ike
Answer by robertbu · Jan 16, 2013 at 02:33 PM
The DragRigidbody.js comes with Unity (Assets/Import Package/Scripts) and implements this behavior. If I remember correctly they do this with a hinge joint, making the connection on mouse down and releasing it on up.
Your Throw() routine will never be called because grabbed gets set to null before you call Get$$anonymous$$ouseButtonUp(0). You need to restructure your logic a bit in Update().
i fixed it, now it gets called, but it's, still not throwing
Do you have a Rigidbody attached (probably want gravity turned off)? I hacked out the grabbed = null line, and it works for me (with some issues).