So i made this code using several codes found in unity questions, the script is attached to a billboard enemy (doom style)… this is the script
the bullets seem like soap bubbles and they don’t destroy on collisions
using UnityEngine;
using System.Collections;
public class S_skullTower : MonoBehaviour {
public Transform target;
public float turretSpeed;
public float fireRate;
public float fireBallHeight;
public GameObject fireBall;
public float range;
float distance;
private float _lastShotTime = float.MinValue;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//Rotate turret to look at player.
Vector3 relativePos = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
rotation.x=0;
rotation.z=0;
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turretSpeed);
//Fire at player when in range.
distance = Vector3.Distance(transform.position,target.position);
if (distance < range && Time.time > _lastShotTime + (3.0f / fireRate)){
_lastShotTime = Time.time;
launchFireBall();
}
}
void launchFireBall()
{
Vector3 position = new Vector3(transform.position.x, transform.position.y + fireBallHeight, transform.position.z);
Instantiate(fireBall, position, transform.rotation);
Rigidbody rb = fireBall.GetComponent<Rigidbody> ();
rb.velocity = transform.forward * 40;
}
void OnTriggerEnter(Collider fireball)
{
if (fireBall.gameObject.tag == "Player")
{
DestroyObject(fireBall.gameObject);
Debug.Log("Bullet was destroyed");
}
}
}