- Home /
Question by
faggottripti · Jun 26, 2016 at 06:01 AM ·
c#scripting problemscripting beginnershooting
I can't make my player to shoot faster (Check script for reference)
Hello everyone! I was making a player shooting script and was having some trouble. The normal shooting works just fine, but there's a problem with the part where I increase the rate of fire. There is a collider (which is a trigger) in the script, which activates the shooting frenzy for 10 seconds. But when I touch the collider nothing happens. Does anyone know what's wrong here? Thanks. using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
public int damagePerShot = 20;
public float timeBetweenBullets = 0.15f;
public float range = 100f;
public float delay = 1f;
float nextUsage;
float timer;
int time = 0;
Ray shootRay;
RaycastHit shootHit;
int shootableMask;
ParticleSystem gunParticles;
LineRenderer gunLine;
AudioSource gunAudio;
Light gunLight;
float effectsDisplayTime = 0.2f;
void Awake ()
{
shootableMask = LayerMask.GetMask ("Shootable");
gunParticles = GetComponent<ParticleSystem> ();
gunLine = GetComponent <LineRenderer> ();
gunAudio = GetComponent<AudioSource> ();
gunLight = GetComponent<Light> ();
nextUsage = Time.time + delay;
}
void Update ()
{
timer += Time.deltaTime;
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
{
Shoot ();
}
if(timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects ();
}
}
void OnTriggerEnter(Collider c1) //to increase rate of fire
{
if(time <= 10)
{
timeBetweenBullets = 0.05f;
if(Time.time > nextUsage) //to make it execute once per second.
{
nextUsage = Time.time + delay;
time++; //to count seconds
}
}
else
{
timeBetweenBullets = 0.15f;
time = 0;
}
}
public void DisableEffects ()
{
gunLine.enabled = false;
gunLight.enabled = false;
}
void Shoot ()
{
timer = 0f;
gunAudio.Play ();
gunLight.enabled = true;
gunParticles.Stop ();
gunParticles.Play ();
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
Comment
Is Delay
or timeBetweenBullets
the variable that you want to control the speed?
Answer by pronobroy · Jun 26, 2016 at 10:05 AM
I think your shootableMask ignore that ray to collider.Try with out shootableMask for your Physics.Raycast