Question by
bblakeyyy · Jun 07, 2020 at 09:36 PM ·
instantiatebulletturret
Time between bullet shots increasing each time?
Hi there! So i currently have a turret script after following a tutorial but for some reason every time the bullet gets instantiated, the time until the next bullet gets instantiated gets shorter, until bullets are firing every frame. Can anybody help at all? It would be much appreciated! Here is the turret script:
{ public float distance; public float wakeRange; public float ShootInterval; public float bulletSpeed = 100; public float bulletTimer;
public bool awake = false;
public GameObject bullet;
public Transform target;
public Transform shootPoint;
void Awake()
{
}
void Start()
{
}
void Update()
{
RangeCheck();
}
void RangeCheck()
{
distance = Vector3.Distance(transform.position, target.transform.position);
if(distance < wakeRange)
{
awake = true;
}
if (distance > wakeRange)
{
awake = false;
}
}
public void Attack(bool attacking)
{
bulletTimer += Time.deltaTime;
if(bulletTimer >= ShootInterval)
{
Vector2 direction = target.transform.position - transform.position;
direction.Normalize();
if (!attacking)
{
GameObject bulletClone;
bulletClone = Instantiate(bullet, shootPoint.transform.position, shootPoint.transform.rotation) as GameObject;
bulletClone.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
bulletTimer = 0;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Changing rotation of prefab 0 Answers
Bullets are firing in the wrong direction. 1 Answer
randomly shoot using number generator 0 Answers
Moving a spawned shuriken 2 Answers