I need constant fire from a ‘turret’ to shoot at the player. Right now it’s not shooting at all. Where am I going wrong?
The code for the bullets is below…
using UnityEngine;
using System.Collections;
public class BulletScript : MonoBehaviour
{
public AudioClip collisionPop;
public float bulletSpeed = 15f;
public float bulletLife = 2f;
Rigidbody2D rb2D;
float spawnTime;
bool bulletActive = false;
public GameObject particle;
public Transform splatter;
public int numberParticles = 10;
// Use this for initialization
void Start()
{
spawnTime = Time.time;
rb2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Time.time > spawnTime + bulletLife) Destroy(gameObject);
}
// Think this is where I'm going wrong?
void FixedUpdate()
{
Vector2 currentDirection = new Vector2((float)Mathf.Cos(rb2D.rotation * Mathf.Deg2Rad), (float)Mathf.Sin(rb2D.rotation * Mathf.Deg2Rad));
rb2D.MovePosition(rb2D.position + currentDirection * bulletSpeed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D other)
{
bulletActive = true;
if (other.gameObject.tag == "Player")
{
GetComponent<AudioSource>().PlayOneShot(collisionPop);
GetComponentInChildren<SpriteRenderer>().enabled = false;
Destroy(gameObject, collisionPop.length); // destroy after audio finishes
for (int i = 0; i < numberParticles; i++)
{
splatter.rotation = Random.rotationUniform;
Instantiate(particle, transform.position, splatter.rotation);
}
}
}
}