I want the player to be able to fire three slow moving projectiles with the right mouse button and then teleport to them, one after the other (as long as they still exist) using the left mouse button. This is presenting two specific challenges for me. First, I don’t know how to access/store the position of the instantiated projectiles, so that my player can teleport to them.
You’ll see below that I tried using a tag to find the object but I’m very new to Unity and C# and I am having difficulty.
Secondly, I want to have it so that there can be a maximum of three projectiles at any point. Creating a new projectile once there are already three should destroy the oldest and replace it with the new one. The tricky part for me (even the logic) is that if, say the second projectile is prematurely destroyed (by say colliding with an object), how do I maintain the proper order when adding a new one. In other words, I always want to teleport to the oldest projectile.
Eg. I fire all three projectiles. Projectile1 and Projectile3 both still exist but Projectile two was triggered when it collided with an object and destroyed itself. Now I fire another projectile. I need it to be come after Projectile1 and Projectile3 so that they’ll be used first.
After doing some research I’m thinking an array may be the answer but again, I’m just too new to this to know quite how to approach the problem.
using UnityEngine;
using System.Collections;
public class FireSynapse : MonoBehaviour {
public float cooldown = 1f;
float cooldownRemaining = 0;
public GameObject SynapsePrefab;
//used for initialization
void Start () {
}
//update called once per frame
void Update () {
cooldownRemaining -= Time.deltaTime;
if (Input.GetMouseButtonDown(1) && cooldownRemaining <= 0) {
cooldownRemaining = cooldown;
Instantiate(SynapsePrefab, Camera.main.transform.position, Camera.main.transform.rotation);
}
if (Input.GetMouseButtonDown (0)) {
GameObject Portal = GameObject.FindWithTag("Portal");
Debug.Log (transform.position = new Vector3(SynapsePrefab.transform.position.x, SynapsePrefab.transform.position.y, SynapsePrefab.transform.position.z));
}
}
}
Thank you in advance for your help.
PS: I hope that didn’t count as two questions. I apologize if it did and I can re-post that second part of my dilemna elsewhere if need be. Thanks again.