Shoot / Spawn Left or Right

hi & thanks for looking at my question.

sp i had the following script i’ve been using that shoots a prefab from the object (right side for reference) the script is attached too. works a treat.

using UnityEngine;
using System.Collections;

public class _BallLauncher : MonoBehaviour {
    public Rigidbody projectile;
	public float fireRate = 0.5F;
    private float nextFire = 0.0F;
    void Update() {
		if (Input.GetButtonDown("Fire1")&& Time.time > nextFire) {
			Rigidbody clone;
            clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            clone.velocity = transform.TransformDirection(Vector3.forward * 35);
       		clone.rotation = Quaternion.identity;
			clone.tag = "RightMarble";
            
        }
	}

}

now what i’d like to do is extend the script to shoot once from the right spot , and take next shot from the left spot and so on. the left spot also has the script attached to it. i have the logic of the script trace out correct ‘true’ and ‘false’ via debug. currently both spawn points shoot at the same time and i would like them to alternate, take turns who shoots. script i have till now is

using UnityEngine;

using System.Collections;

public class _BallLauncher : MonoBehaviour {
public Rigidbody projectile;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
public bool SideSelectLeft = false;

void Update() {
	if (Input.GetButtonDown("Fire1")&& Time.time > nextFire) {
		if( SideSelectLeft != true){
			Debug.Log("SideSelectLeft false:"+SideSelectLeft);
			Rigidbody cloneR;
            cloneR = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            cloneR.velocity = transform.TransformDirection(Vector3.forward * 35);
       		cloneR.rotation = Quaternion.identity;
			cloneR.tag = "RightMarble";
			SideSelectLeft = true;
		} else {
			Debug.Log("SideSelectLeft true:"+SideSelectLeft);
			Rigidbody cloneL;
            cloneL = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            cloneL.velocity = transform.TransformDirection(Vector3.forward * 35);
       		cloneL.rotation = Quaternion.identity;
			cloneL.tag = "LeftMarble";
			SideSelectLeft = false;
		}
        
    }
}

}

logically i understand why the script wont work this way, as the prefab is spawning from the point of what the script is attached to. so am i better off attaching the code to an empty gameobject and referencing which spawn point to use next? or can i edit the script to have it working just attached to the two spawn points as it currently is. what the best approach?

sorry for the lengthy explanation.

I would think it would just be easiest to reference the two alternating points in which the prefab would spawn. And just get their location everytime you fire out of that side. I came across the same problem actually and was doing the exact same thing in one of my games :slight_smile: It’s just sudjestion

this is how i solved it :

using UnityEngine;
using System.Collections;

public class _BallLauncher : MonoBehaviour
{
public Rigidbody projectileL;
public Rigidbody projectileR;
public GameObject LeftLauncher;
public GameObject RightLauncher;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
public bool SideSelectLeft = true;

void Update ()
{
	if (Input.GetButtonDown ("Fire1") && Time.time > nextFire) {
			if (SideSelectLeft != true) {
				Debug.Log ("SideSelectLeft false:" + SideSelectLeft);
				Rigidbody cloneR;
				cloneR = Instantiate (projectileR, RightLauncher.transform.position, transform.rotation) as Rigidbody;
				cloneR.velocity = transform.TransformDirection (Vector3.forward * 35);
				cloneR.rotation = Quaternion.identity;
				cloneR.tag = "RightMarble";
				SideSelectLeft = true;
			} else {
				Debug.Log ("SideSelectLeft true:" + SideSelectLeft);
				Rigidbody cloneL;
				cloneL = Instantiate (projectileL, LeftLauncher.transform.position, transform.rotation) as Rigidbody;
				cloneL.velocity = transform.TransformDirection (Vector3.forward * 35);
				cloneL.rotation = Quaternion.identity;
				cloneL.tag = "LeftMarble";
				SideSelectLeft = false;
		}
        
	}
}

}

I had the same issue, here is my solution.

On your object, you need two empty game objects, one called LeftBarrel and one called RightBarrel

var LBorRB : int;
LBorRB = 0;
var firefrom;
var RGkinetic : GameObject;
var RGflack : GameObject;
function Start() {
RGkinetic = (Resources.Load("Ammo/RailGun/prefabs/RGkinetic"));
RGflack = (Resources.Load("Ammo/RailGun/prefabs/RGflack"));
}


function Update () {
// lmb was pressed, launch a projectile
if (Input.GetButtonDown("lmb")) {
// Instantiate the projectile at the position and rotation of this transform
var clone : GameObject;
clone = Instantiate(RGflack, transform.position, transform.rotation);


// Give the cloned object an initial velocity along the current
// object's Z axis
if (LBorRB % 2 == 0) {
firefrom = transform.FindChild("LeftBarrel");
} else {
firefrom = transform.FindChild("RightBarrel");

}
clone.transform.position = firefrom.transform.position;
clone.rigidbody.AddRelativeForce(Vector3.forward * 1000);
LBorRB ++;
 }
 }

As long as you instantiate the script you are able to avoid all items firing at once :smiley: