Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by El-Deiablo · Aug 30, 2016 at 08:35 PM · animationinstantiatespawnreferencingspawnpoints

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 );
 
     }
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image El-Deiablo · Aug 31, 2016 at 12:52 AM 0
Share

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);
 
     }
 
 }
avatar image b1gry4n El-Deiablo · Aug 31, 2016 at 12:58 AM 0
Share

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);
 }
avatar image El-Deiablo b1gry4n · Aug 31, 2016 at 01:31 AM 0
Share

Didn't know about that. Thanks for sharing! Will give this try.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges