- Home /
Don't allow the player to shoot while projectile is travelling
Hi, I would like the player to be unable to shoot while the projectile is travelling. I've got collisions set up for when the bullet hits something. I know I can do this using booleans, but I'm not entirely sure how.
Gun Script:
using UnityEngine;
using System.Collections;
public class ProjectileCS : MonoBehaviour
{
//public variables
public Rigidbody projectile;
public float speed = 20.0f;
public Rigidbody instantiateProjectile;
// Update is called once per frame
void Update ()
{
//input
if (Input.GetButtonDown ("Fire1"))
{
instantiateProjectile = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody; //Object to clone, the position, the rotation
//Moving projectile
instantiateProjectile.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));
}
}
}
Any help would be great.
Comment
Answer by arklay_corp · Mar 10, 2015 at 09:38 AM
You have many choices, for example:
Create a Bullet class derived from MonoBehaviour
Add it to the projectile prefab
Add a event in the bullet class
OnCollision or OnTrigger, when it happens fire the event
In the ProjectileCS listen to that event