There are a lot of tutorials for throwing objects in a fps game (for example:
)
However, these tutorials are for throwing the object in the direction the camera is looking. But, my camera is stable and it always looks forward. I want to throw objects to the direction the user clicks.
Here is some part of a code I found online. This is for throwing in the direction the camera is looking:
private void Throw()
{
readyToThrow = false;
// instantiate object to throw
GameObject projectile = Instantiate(objectToThrow, attackPoint.position, cam.rotation);
// get rigidbody component
Rigidbody projectileRb = projectile.GetComponent<Rigidbody>();
// calculate direction
Vector3 forceDirection = cam.transform.forward;
RaycastHit hit;
if(Physics.Raycast(cam.position, cam.forward, out hit, 500f))
{
forceDirection = (hit.point - attackPoint.position).normalized;
}
// add force
Vector3 forceToAdd = forceDirection * throwForce + transform.up * throwUpwardForce;
projectileRb.AddForce(forceToAdd, ForceMode.Impulse);
totalThrows--;
// implement throwCooldown
Invoke(nameof(ResetThrow), throwCooldown);
}
Probably all I have to do is to change here: Vector3 forceDirection = cam.transform.forward;
But I don’t know how i can give the positions of which way the user clicks