I’ve searched all over to find a way to do this and spent 2 days doing so with no luck.
So far, I have an x-wing style ship with 4 laser turrets. I have a script to fire all 4 at once, but I need to find a way, to switch between 3 firing modes…all 4, 2 turrets alternating with the other 2 and one after the other in order.
This is the script for all 4 (could this be done any tidier also?)…
using UnityEngine;
using System.Collections;
public class XW_Shooting : MonoBehaviour {
public GameObject bullet_prefab;
public float bulletImpulse = 20f;
public float xwFireRate = 1f;
private float xwFire = 0.0f;
Transform gunTip1;
Transform gunTip2;
Transform gunTip3;
Transform gunTip4;
public AudioClip shoot;
void Start ()
{
gunTip1 = GameObject.Find ("GuntipLT").transform;
gunTip2 = GameObject.Find ("GuntipRT").transform;
gunTip3 = GameObject.Find ("GuntipLB").transform;
gunTip4 = GameObject.Find ("GuntipRB").transform;
}
// Update is called once per frame
void Update (){
if (Input.GetButton ("Fire1") && Time.time > xwFire) {
xwFire = Time.time + xwFireRate;
GameObject thebullet = (GameObject)Instantiate (bullet_prefab, gunTip1.transform.position + transform.forward, transform.rotation);
thebullet.rigidbody.AddForce (transform.forward * bulletImpulse, ForceMode.Impulse);
audio.PlayOneShot(shoot);
gunTip2 = GameObject.Find ("GuntipRT").transform;
GameObject thebullet2 = (GameObject)Instantiate (bullet_prefab, gunTip2.transform.position + transform.forward, transform.rotation);
thebullet2.rigidbody.AddForce (transform.forward * bulletImpulse, ForceMode.Impulse);
audio.PlayOneShot(shoot);
gunTip3 = GameObject.Find ("GuntipLB").transform;
GameObject thebullet3 = (GameObject)Instantiate (bullet_prefab, gunTip3.transform.position + transform.forward, transform.rotation);
thebullet3.rigidbody.AddForce (transform.forward * bulletImpulse, ForceMode.Impulse);
audio.PlayOneShot(shoot);
gunTip4 = GameObject.Find ("GuntipRB").transform;
GameObject thebullet4 = (GameObject)Instantiate (bullet_prefab, gunTip4.transform.position + transform.forward, transform.rotation);
thebullet4.rigidbody.AddForce (transform.forward * bulletImpulse, ForceMode.Impulse);
audio.PlayOneShot(shoot);
}
}
}
Thanks for looking!
This might break with "Not all code paths return a value". Put a
– Kiwasiyield return nullin at the end of the coroutine to fix.I'm sorry I didn't mention the key press thing beforehand. Very kind of you to continue to help me :) I shall try and incorporate these ideas into the script. I will look into arrays though, as I'm sure it could be less cluttered. It may even help with the audio being played in the correct 3d space...that's another thread though. Cheers
– MajorParts