- Home /
Storing the Position Generated by Random.Range
In my game there are 6 spawn points - 3 on the left and 3 on the right. I have my left spawner script randomly choose 1 of the 3 spawn points on the left and then spawn a cross hair (which acts as a warning). The cross hair has an animation attached to it that moves right and then back (might have something to do with issue). After the warning a bullet spawns and moves across the screen. I can get both prefabs to instantiate, but instantiating them in the same position is not working out. I need to find a way to reference the spawn point chosen for the cross hairs and then reuse the randomly chosen spawnpoint for my bullet. I've tried a couple things, but haven't been able to get it to work properly.
Here's my code:
using UnityEngine;
using System.Collections;
public class BulletSpawnerLeft : MonoBehaviour {
public Transform [] spawnPoints;
//public Transform [] crossHairSpawnPoints;
public float spawnTime;
public GameObject pistolBullet;
public GameObject[] pistolAndShotgunBullet;
public GameObject[] pistolShotgunAndSniperBullet;
public GameObject[] pistolShotgunSniperAndRocketBullet;
//public static bool isActive = false;
public GameObject crossHairs;
private int level1 = 0;
private int level2 = 0;
private int level3 = 0;
void Start(){
spawnTime = 7f;
InvokeRepeating ("SpawnPistolBullet", spawnTime, spawnTime);
}
void Update(){
if (ScoreManager.scoreCount >= 5000 && ScoreManager.scoreCount < 10000 && level1 == 0) {
level1++;
spawnTime = 6f;
CancelInvoke ("SpawnPistolBullet");
InvokeRepeating ("SpawnPistolAndShotgunBullet", spawnTime, spawnTime);
}
if (ScoreManager.scoreCount >= 10000 && ScoreManager.scoreCount < 20000 && level2 == 0) {
level2++;
spawnTime = 4f;
CancelInvoke ("SpawnPistolAndShotgunBullet");
InvokeRepeating ("SpawnPistolShotgunAndSniperBullet", spawnTime, spawnTime);
}
if (ScoreManager.scoreCount > 20000 && level3 == 0) {
level3++;
spawnTime = 2f;
CancelInvoke ("SpawnPistolShotgunAndSniperBullet");
InvokeRepeating ("SpawnPistolShotgunSniperAndRocketBullet", spawnTime, spawnTime);
}
}
void SpawnPistolBullet(){
int spawnIndex= Random.Range (0, spawnPoints.Length);
//Transform crossHairs;
//crossHairs = Random.Range (0, spawnPoints.Length);
GameObject crossHairs1;
crossHairs1 = Instantiate (crossHairs, spawnPoints[spawnIndex].position, spawnPoints[spawnIndex].rotation)as GameObject;
//isActive = true;
if (crossHairs1.transform.position == spawnPoints[0].transform.position) {
Instantiate (pistolBullet, crossHairs1.transform.position, crossHairs1.transform.rotation);
}
else if (crossHairs1.transform.position == spawnPoints[1].transform.position) {
Instantiate (pistolBullet, crossHairs1.transform.position, crossHairs1.transform.rotation);
}
else if (crossHairs1.transform.position == spawnPoints[2].transform.position) {
Instantiate (pistolBullet, crossHairs1.transform.position, crossHairs1.transform.rotation);
}
/*if (spawnPoints [0]) {
Instantiate (crossHairs, crossHairSpawnPoints [0].position, crossHairSpawnPoints [0].rotation);
Destroy (crossHairs, 2);
}*/
}
void SpawnPistolAndShotgunBullet(){
int spawnIndex = Random.Range (0, spawnPoints.Length);
int objectIndex = Random.Range (0, pistolAndShotgunBullet.Length);
Instantiate (pistolAndShotgunBullet[objectIndex], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation );
}
void SpawnPistolShotgunAndSniperBullet(){
int spawnIndex = Random.Range (0, spawnPoints.Length);
int objectIndex = Random.Range (0, pistolShotgunAndSniperBullet.Length);
Instantiate (pistolShotgunAndSniperBullet[objectIndex], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation );
}
void SpawnPistolShotgunSniperAndRocketBullet(){
int spawnIndex = Random.Range (0, spawnPoints.Length);
int objectIndex = Random.Range (0, pistolShotgunSniperAndRocketBullet.Length);
Instantiate (pistolShotgunSniperAndRocketBullet[objectIndex], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation );
}
}
Answer by b1gry4n · Aug 31, 2016 at 12:46 AM
private Vector3 lastspawnpointused;
store the last spawnpoint used based off which spawnpoint was chosen
lastspawnpointused = spawnPoints[spawnIndex].position;
now whenever you need to reference the last spawnpoint
bullet.position = lastspawnpointused;
Your solution definitely answers the question. I actually figured out my own route and realized that my animation was in a specific location. Whenever I would instantiate my cross hairs, regardless of the spawn position, the cross hairs would spawn where the animation took place. I created three separate cross hairs and made sure the animation on each started in the same position as the spawn points. Here's what I came up with:
void SpawnPistolBullet(){
int spawnIndex = Random.Range (0, 3);
Debug.Log (spawnIndex);
if (spawnIndex==0) {
Debug.Log ("0");
Instantiate (crossHairs1, spawnPoint1.transform.position, spawnPoint1.transform.rotation);
StartCoroutine (waitTime1 ());
}
else if (spawnIndex==1) {
Debug.Log ("1");
Instantiate (crossHairs2, spawnPoint2.transform.position, spawnPoint2.transform.rotation);
StartCoroutine (waitTime2 ());
//Instantiate (pistolBullet, spawnPoint2.position, spawnPoint2.rotation);
}
else if (spawnIndex==2) {
Debug.Log ("2");
Instantiate (crossHairs3, spawnPoint3.transform.position, spawnPoint3.transform.rotation);
StartCoroutine (waitTime3 ());
//Instantiate (pistolBullet, spawnPoint3.position, spawnPoint3.rotation);
}
}
public IEnumerator waitTime1(){
yield return new WaitForSeconds (2);
Instantiate (pistolBullet, crossHairs1.transform.position, crossHairs1.transform.rotation);
}
public IEnumerator waitTime2(){
yield return new WaitForSeconds (2);
Instantiate (pistolBullet, crossHairs2.transform.position, crossHairs2.transform.rotation);
}
public IEnumerator waitTime3(){
yield return new WaitForSeconds (2);
Instantiate (pistolBullet, crossHairs3.transform.position, crossHairs3.transform.rotation);
}
}
If it works it works, but I would have tackled this a little differently than having to make different animations for each spawn point. What if you want to add more later? Youll have to set up more animations for each spawn point. Ins$$anonymous$$d I would instantiate the crosshair, set it to a child of the spawnpoint with a localposition of Vector3.zero, and have its position lerp to the desired point you want it "animated" to in local space.
public float crosshairspeed = 2.0f;
void Update(){
Vector3 desiredLocation = new Vector3 (10,0,0); // or wherever you want it moved
crosshair.localPosition = Vector3.Lerp(crosshair.localPosition, desiredLocation, Time.deltaTime * crosshairspeed);
}
Didn't know about that. Thanks for sharing! Will give this try.
Answer by JoshDangIt · Aug 30, 2016 at 10:51 PM
I may not have read your question right, but how about instantiating the bullet from the cross hair gameobject? Make a script that Instantiates the bullet and attach it to the animation you're instantiating.
Your answer
Follow this Question
Related Questions
one scene multiple spawn points 1 Answer
Network.Instantiate players at each spawnpoint? Don't use the same spawnpoint? 1 Answer
Prevent object from spawning at a spawn point twice in a row? 3 Answers
How do I control where enemies spawn? 1 Answer
How do I spawn an object under another moving object at random in C#? 1 Answer