Constant fire at target

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);
            }
        }
    }

}

@Chris When I run the game, no bullets are firing at the player at all. I have the turrets following the position of the player, but there are no bullets coming out of them :confused:

You will need a script that handles:

  • The creation / instantiation of the bullets

  • Place the bullet at the turret’s barrel position

  • Rotate the bullet to the barrel’s rotation

  • Set it’s velocity to a high value, so the bullet goes fast

  • Set the rigidbody’s property : “UseGravity” to false, so it doesn’t fall.

  • For that you will need an empty game object that will represent the barrel’s position and orientation.

After that, in your Bullet script, you delete the FixedUpdate() method, as you won’t need to move the bullet manually, it’s going to move alone, here it’s an example of such a script:

using UnityEngine;
using System.Collections;

public class FireBullets : MonoBehaviour {

	[SerializeField] float roundsPerMinute = 500;	// Set the fire rate to 500 bullets per minute.
	[SerializeField] float bulletSpeed = 100;
	[SerializeField] Transform barrel;				// Assign the barrel position in inspector.
	[SerializeField] Rigidbody2D bulletPrefab;		// Assign a bullet prefab here, 
													// it must have a Rigidbody component attached

	float timeBetweenShots;
	float nextTimeCanShoot;
	
	
	void Start() {
		// Get the time between shots in seconds.
		timeBetweenShots = 60.0f / roundsPerMinute;
	}
	
	void Update() {
		// If the player holds the left mouse button.
		if(Input.GetMouseButton(0)) {
			// We try to shoot.
			TryShoot();
		}
	}
	
	void TryShoot() {
		// If not enough time has passed between shots, stop the function.
		if(Time.time < nextTimeCanShoot) {
			return;
		}
		
		// Create the bullet at the barrel's position and rotation.
		Rigidbody2D bullet = Instantiate(bulletPrefab, barrel.position, barrel.rotation) as Rigidbody2D;
		bullet.gravityScale = 0;
		bullet.velocity = new Vector2(barrel.forward.x, barrel.forward.y) * bulletSpeed;
	}
}