How to move the rigidbody at the position of the mouse on release
Posted
by
Edvin
on Game Development
See other posts from Game Development
or by Edvin
Published on 2014-05-31T09:08:22Z
Indexed on
2014/05/31
16:06 UTC
Read the original article
Hit count: 184
unity
I'm making a "Can Knockdown" game and I need the rigidbody to move where the player released the mouse(OnMouseUp). Momentarily the Ball moves OnMouseUp because of rigidbody.AddForce(force * factor); and It moves toward the mousePosition but doesn't end up where the mousePosition is. Here's what I have so far in the script.
var factor = 20.0;
var minSwipeDistY : float;
private var startTime : float;
private var startPos : Vector3;
function OnMouseDown(){
startTime = Time.time;
startPos = Input.mousePosition;
startPos.z = transform.position.z - Camera.main.transform.position.z;
startPos = Camera.main.ScreenToWorldPoint(startPos);
}
function OnMouseUp(){
var endPos = Input.mousePosition;
endPos.z = transform.position.z - Camera.main.transform.position.z;
endPos = Camera.main.ScreenToWorldPoint(endPos);
var force = endPos - startPos;
force.z = force.magnitude;
force /= (Time.time - startTime);
rigidbody.AddForce(force * factor);
}
© Game Development or respective owner