Reputation: 19
I'm trying to make a grenade throwing script but when i test it, it always spawn 2 grenade at the same time.
public class GrenadeThrow : MonoBehaviour {
public GameObject bulletprefab;
float speed =20f;
// Use this for initialization
void Start () { }
// Update is called once per frame
void Update () {
if (Input.GetButtonUp("Fire1"))
{
Camera cam = Camera.main;
GameObject Grenade = Instantiate(bulletprefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
Grenade.GetComponent<Rigidbody>().AddForce(cam.transform.forward * speed, ForceMode.Impulse);
}
}
}
Upvotes: 0
Views: 1252
Reputation: 125245
First of all, Input.GetButtonDown
is more appropriate for this than Input.GetButtonUp
. You can try it and see if Input.GetButtonDown
is still what you want.
when i test it, it always spawn 2 grenade at the same time
Assuming that this is the actual code you are using to spawn and throw Objects, then it should work fine.
There two likely problems:
1.The GrenadeThrow
script is likely attached to the-same GameObject multiple times.
2.The problem is likely to be that you have your GrenadeThrow
script attached to multiple GameObjects. It should only be attached to one GameObject.
Upvotes: 1