I'm trying to add a force to the Rigidbody
component of an instantiated projectile in Unity. I want to use this method as it is a simple throw mechanic and I just want the projectile to move in a small parabolic trajectory. The projectile prefab has already been attached to this script in the Unity editor and it does have a Rigidbody component.
Here's my code so far:
private void ProjectileShoot()
{
if (Input.GetKeyDown(KeyCode.LeftShift) && !gameOver)
{
Rigidbody projectileRb = projectilePrefab.GetComponent<Rigidbody>();
Instantiate(projectilePrefab, transform.position,
projectilePrefab.transform.rotation);
projectileRb.AddForce(throwForce * Vector3.forward, ForceMode.Impulse);
}
}
The projectile prefab spawns at the player's location as expected but it just falls to the ground straight away. No matter how much throwForce
I apply to it.
ProjectileShoot()
is in Update()
by the way.
I am very much an amateur at C# and Unity and this is my first project so any help is appreciated. Thank you.