I am new to Unity3D.
I am working on a game in which i need to throw a ball to target bucket but that won’t be a direct drop in a bucket, i need one drop in between and then 2nd drop will be in bucket ( like bowling ).
My camera is set as ball and target bucket will get distance in z axis. I tried different approaches but those will work in either to much of force or direct drop in bucket.
Code :
private float factor = 50.0f;
void OnMouseDown()
{
startTime = Time.time;
startPos = Input.mousePosition;
startPos.z = transform.position.z - Camera.main.transform.position.z;
startPos = Camera.main.ScreenToWorldPoint(startPos);
}
void OnMouseUp()
{
endPos = Input.mousePosition;
endPos.z = transform.position.z - Camera.main.transform.position.z;
endPos = Camera.main.ScreenToWorldPoint(endPos);
force = endPos - startPos;
force.z = force.magnitude;
force /= (Time.time - startTime);
GetComponent<Rigidbody>().AddForce(force * factor);
}
The problem with my code is, it adds too much force in z direction so the drop will be far away after the target bucket and force value for y direction is very low so it won’t get in to target bucket.
So i need help to manage y and z axis force depending on swipe in a way so it look realistic.
Thanks for your help in advance.