• 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 /
  • Help Room /
avatar image
0
Question by Raviraja190 · Oct 24, 2016 at 11:01 AM · playerspawningspawning problems

player spawning somewhere distance in the field?

using UnityEngine; using System.Collections;

public class CloudSpawner : MonoBehaviour {

 [SerializeField]
 public GameObject[] clouds;
 private float distanceBetweenClouds=3f;
 private float minX, maxX;
 private float lastcloudPositionY;
 private float controlX;
 [SerializeField]
 public GameObject[] collectables;
 private GameObject Player;

 void SetMinAndMaxX(){
     Vector3 bounds = Camera.main.ScreenToWorldPoint (new Vector3 (Screen.width, Screen.height, 0));
     maxX = bounds.x - 0.5f;
     minX = -bounds.x + 0.5f;
 }
 void Shuffle(GameObject[] arrayToshuffle){
     for(int i=0; i<arrayToshuffle.Length; i++){
         GameObject temp = arrayToshuffle [i];
         int random = Random.Range(i,arrayToshuffle.Length);
         arrayToshuffle [i] = arrayToshuffle [random];
         arrayToshuffle[random]=temp;
     }
 }
 void Awake(){
     controlX=0;
     SetMinAndMaxX();
     CreateClouds();
     Player = GameObject.Find ("Player");

     for (int i = 0; i < collectables.Length; i++) {
         collectables[i].SetActive (false);
     }
 }
 void Start (){
  PositionThePlayer();
 }

     void CreateClouds(){
         Shuffle(clouds);
         float positionY = 0f;
     for (int i=0; i< clouds.Length; i++){
             Vector3 temp = clouds[i].transform.position;
         temp.y = positionY;
         if (controlX == 0) {
             temp.x = Random.Range (0.0f, maxX);
             controlX = 1;
         } else if (controlX == 1){
                 temp.x= Random.Range(0.0f,minX);
                 controlX =2;
             }
         else if (controlX ==2){
                 temp.x=Random.Range(1.0f,maxX);
                 controlX=3;
             } 
             else if (controlX ==3){
                 temp.x =Random.Range(-1.0f,minX);
                 controlX=0;
             }

     lastcloudPositionY= positionY;
             clouds[i].transform.position =temp;
             positionY -= distanceBetweenClouds;
         }
     }
 void PositionThePlayer(){
     GameObject[] darkClouds = GameObject.FindGameObjectsWithTag ("Deadly");
     GameObject[] cloudsInGame= GameObject.FindGameObjectsWithTag("Cloud");

     for (int i = 0; i < darkClouds.Length; i++)
     {
         if(darkClouds[i].transform.position.y == 0f){
             Vector3 t= darkClouds[i].transform.position;
             darkClouds[i].transform.position=new Vector3 (cloudsInGame[0].transform.position.x,
                                                           cloudsInGame[0].transform.position.y,
                                                           cloudsInGame[0].transform.position.z);
         cloudsInGame[0].transform.position = t;
         }
     }
     Vector3 temp =cloudsInGame [0].transform.position;
     for(int i=1; i<cloudsInGame.Length; i++){
         if (temp.y<cloudsInGame[i].transform.position.y){
             temp = cloudsInGame [i].transform.position;
         } 
     }
     temp.y += 0.8f;
     Player.transform.position=temp;
 }

 void OnTriggerEnter2D(Collider2D target){
     if(target.tag =="Cloud"|| target.tag =="Deadly")
     {
         if(target.transform.position.y   == lastcloudPositionY){
             Shuffle(clouds);
             Shuffle(collectables);

             Vector3 temp= target.transform.position;
             for(int i=0; i<clouds.Length;i++){
                 
                 if(!clouds[i].activeInHierarchy){
                     if(controlX == 0){
                         temp.x = Random.Range(0.0f, maxX);
                         controlX=1;
                     } else if (controlX == 1){
                         temp.x= Random.Range(0.0f,minX);
                         controlX =2;
                     }
                     else if (controlX ==2){
                         temp.x=Random.Range(1.0f,maxX);
                         controlX=3;
                     }
                     else if (controlX ==3){ 
                         temp.x =Random.Range(-1.0f,minX);
                         controlX=0;
                     }
                     temp.y -= distanceBetweenClouds;
                     lastcloudPositionY=temp.y;
                     clouds[i].transform.position=temp;
                     clouds[i].SetActive(true);
                 
                 int random = Random.Range (0, collectables.Length);
                 if(clouds[i].tag !="Deadly"){
                     if(!collectables[random].activeInHierarchy){
                             
                         Vector3 temp2=clouds[i].transform.position;
                         temp2.y +=0.7f;

                         if(collectables[random].tag=="Life"){

                             if(PlayerScoreScript.lifeCount<2){
                                 collectables[random].transform.position=temp2;
                                 collectables[random].SetActive(true);
                             }
                             else{
                                 collectables[random].transform.position=temp2;
                                 collectables[random].SetActive(true);
             }
         
             }
             }
             

} } } } } } }

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

79 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

Related Questions

Why is the Player Transform only changing correctly the first time? 1 Answer

problem when i spawn to many Enemy the game be slower 1 Answer

Putting a cap on enemies spawned 2 Answers

How to spawn multiple prefabs at different spawn rates 0 Answers

Instantiate along the Z/X Axis only? 0 Answers


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