How to move the rigidbody at the position of the mouse on release
- by Edvin
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);
}