throw game object along the curve with a swipe

Viewed 24

I'm making a mobile game in Unity3D. And there I wanted to make it so that I could throw object along the curve with a swipe. If I do it with AddForce, it only flies in a straight line, and I want it to spin like in the photo. Help, please

In this case object flies in a straight line

void OnMouseDown()
{
 //get soccer ball starting position when swiping
 startPos = Input.mousePosition;
 startPos.z = transform.position.z - Camera.main.transform.position.z;
 startPos = Camera.main.ScreenToWorldPoint(startPos);
}

void OnMouseUp()
{
 //get soccer ball end position when swipe finished
 Vector3 endPos = Input.mousePosition;
 endPos.z = transform.position.z - Camera.main.transform.position.z;
 endPos = Camera.main.ScreenToWorldPoint(endPos);

//get the proper direction and z force
Vector3 force = endPos - startPos;
force.z = force.magnitude * 3;
force.x = force.x * 1.5f;

//check if you swiped (not just clicked) and if ball had not been fired already
if (startPos != endPos && GetComponent<Rigidbody>().isKinematic == true)
{
    GetComponent<Rigidbody>().isKinematic = false;
    GetComponent<Rigidbody>().AddForce(force * Force);
    gameObject.transform.Rotate(new Vector3(0, 0, force.z / 3));
    
    //destroy ball after some time and add a new one to shoot
    StartCoroutine(destroyBall());
    StartCoroutine(newBall());
}

}

0 Answers
Related