Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

4 sprite change

Discussion in 'Scripting' started by Magz-Studios, Apr 17, 2015.

  1. Magz-Studios

    Magz-Studios

    Joined:
    Apr 9, 2015
    Posts:
    5
    Hey Everyone!
    I have a quick question. Trying to make a sprite spawner. I have some code just need few changes.
    Thanks!
     
    Last edited: Oct 6, 2015
  2. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    You could create a SpriteManager script that contains a public array of GameObjects - fill the array from the inspector with your sprite prefabs, and handle the sprites from there. So..to get you started:

    Code (CSharp):
    1. public GameObject[] sprites; //these sprites are dragged and dropped into the array from the inspector. The sprites should be prefabs
    2.  
    3. public float spawnFrequency = 1;
    4. float spawnTimer = 0;
    5.  
    6. void Update()
    7. {
    8.      spawnTimer += Time.deltaTime;
    9.      if (spawnTimer >= spawnFrequency)
    10.      {
    11.            SpawnSprite();
    12.            spawnTimer = 0;
    13.      }
    14. }
    15.  
    16. void SpawnSprite()
    17. {
    18.       int random; //add a random value from 0 to 3 into this value
    19.      GameObject s = Instantiate (sprites[random] as GameObject);
    20.      //then set the postion of s wherever you want it to start
    21.      s.transform.position = yourDesiredPosition;
    22. }
    Each sprite prefab will need a script on it to (A) move the sprite the direction you want and (B) check to see if the sprite hit the left wall.

    Hope this points you in the right direction! :)
     
  3. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    Are they not moving out of the way when they are spawned?
     
  4. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    You want the sprites to continuously move to the left when they are spawned right?
     
  5. Magz-Studios

    Magz-Studios

    Joined:
    Apr 9, 2015
    Posts:
    5
    Its either going to be left or down. But that should be easily changeable.
     
  6. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    Ok, create a new script called SpriteController.cs and attach it to your prefabs. The code should look something like this:

    Code (CSharp):
    1.  
    2. public Vector3 direction = new Vector3(-1, 0, 0);
    3. public float speed = 5f;
    4.  
    5. bool alive = true;
    6.  
    7. void Update()
    8. {
    9.      transform.Translate(direction * speed * Time.deltaTime);
    10.      CheckOffScreen();
    11.      if (!alive)
    12.      {
    13.           Destroy(gameObject);
    14.      }
    15. }
    16.  
    17. void CheckOffScreen()
    18. {
    19.      //check the position you want to do something. if you want to delete the sprite, you would set alive to false
    20. }
    21.  
    22.  
     
  7. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    Ok, now I see. In that case get rid of the transform.Translate line of code.

    Go back to the InfinityBalls script and add:

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3.  
    4. public float offset = 5f;
    5.  
    6. List<GameObject> balls;
    7.  
    8. void Start()
    9. {
    10.      balls = new List<GameObject>();
    11. }
    12. void SpawnSprite()
    13. {
    14.      int random = Random.Range(0, 4);
    15.      GameObject s = Instantiate(sprites[random] as GameObject);
    16.      s.transform.position = new Vector3(balls.Count * offset, 0, 0);
    17.      balls.Add(s);
    18. }
    19.  
     
  8. Magz-Studios

    Magz-Studios

    Joined:
    Apr 9, 2015
    Posts:
    5

    Alright,

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class InfintyBalls : MonoBehaviour {
    5.     public GameObject[] sprites;
    6. public float spawnFrequency = 1;
    7. public float offset = 5f;
    8. List<GameObject> balls;
    9. float spawnTimer = 0;
    10. void Start()
    11. {
    12.   balls = new List<GameObject>();
    13. }
    14. void Update()
    15. {
    16.      spawnTimer += Time.deltaTime;
    17.      if (spawnTimer >= spawnFrequency)
    18.      {
    19.            SpawnSprite();
    20.            spawnTimer = 0;
    21.      }
    22. }
    23. void SpawnSprite()
    24. {
    25.       int random = Random.Range (0, 4);
    26.      GameObject s = Instantiate (sprites[random] as GameObject);
    27.  
    28.      s.transform.position = new Vector3(balls.Count * offset, 0, 0);
    29.      balls.Add(s);
    30.  
    31. }
    32. }
    33.  
    this is the code as of now. And the balls aren't moving and it stops spawning after it spawns the first one.
     
  9. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    Code (CSharp):
    1. void SpawnSprite()
    2. {
    3.      int random = Random.Range(0, 4);
    4.      GameObject s = Instantiate (sprites[random] as GameObject);
    5.  
    6.      s.transform.position = Vector3.zero;
    7.      balls.Insert(0, s); //.Add will put new ball at end of list. We want it to be at the beginning for our shift operation
    8.      //shift all balls in list
    9.      for (int i = 0; i < balls.Count; i++)
    10.           balls[i].transform.position = new Vector3(i * offset, 0, 0);
    11. }
    Let me know if it works! :)
     
  10. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    1. You can modify the offset variable to change the distance between each spawned ball.
    2. If one of the balls is destroyed and you want to do another shift, simply run code lines 9 and 10 from above. Perhaps put these in a method and use it to be called from whichever scripts you need.
    3. If you are new to scripting in unity3d, you might give my channel a try (link in my signature). We teach on many fronts and unity3d scripting in c# is one of them.

    Happy coding and keep learning!