So i’ve been working on this mechanic for some time and finally got it to work, I just need the clones to destroy themselves after a few seconds (some bullets will run of into space). Here is my code, thanks!
using UnityEngine;
using System.Collections;
public class Turret3 : MonoBehaviour {
public GameObject bullet;
public GameObject cannonFlash;
private GameObject spawnPt;
// Use this for initialization
void Start () {
spawnPt = GameObject.Find ("oneSpawn");
cannonFlash.SetActive(false);
}
// Update is called once per frame
void Update () {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Input.GetButtonDown ("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit Hit;
if (Physics.Raycast(ray, out Hit, 1000))
{
Debug.DrawLine(ray.origin, Hit.point);
GameObject newProjectile = Instantiate (bullet, spawnPt.transform.position, transform.rotation)as GameObject;
newProjectile.transform.LookAt (Hit.point);
newProjectile.rigidbody.AddRelativeForce(Vector3.up + Vector3.forward * 5000);
Destroy (newProjectile, 5);
print("" + Hit.point);
}
cannonFlash.SetActive(true);
}
if(Input.GetButtonUp ("Fire1"))
{
cannonFlash.SetActive(false);
}
}
}