- Home /
Add force to mouse position?
Hello guys!
I am trying to throw an object where my FPS is facing not just a single direction. In other words where my mouse is pointing. This is my current code:
public GameObject spawnGranade;
public void AddForce(Vector3 direction, float force)
{
GetComponent<Rigidbody>().AddForce(direction * force);
}
void Update() {
if (Input.GetMouseButtonDown(1))
{
Vector3 mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
GameObject crate = (GameObject)Instantiate(spawnGranade, mousePos, Quaternion.identity);
crate.tag = "Granade";
crate.GetComponent<Rigidbody>().AddForce(Vector3.fwd* 500.0f);
}
}
I was thinking to edit my AddForce method to use ray casts and just add them, but I don't think that's the correct way of doing it.
I am guessing this is a simple solution, but I am still a newbie :D
Thanks!
Comment