- Home /
moving starfield with gameobject prefab list
i want to implement a starfield moving from the center of the screen with different speeds of each stars..then beyond screen boundaries they will be produced at the center of the screen..a continuous random movement. i can't make out how those different speeds will be created to my every gameobject in the list/(or in the foreach loop ?) I couldn't even achieve a more accurate motion.
can u pls help ? thanx
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class sfield : MonoBehaviour {
 public GameObject cprefab;
 public List<GameObject> golist;
 public int numG = 100;
 
 public Camera camera;
 private Vector2 screenBounds;
   
 // Start is called before the first frame update
   
 void Start()
 {
    
     screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
     golist = new List<GameObject>();
     for (int i = 0; i < numG; i++)
     {
         golist.Add(Instantiate(cprefab));
         golist[i].transform.position = new Vector3(0.02f + i, 0.02f + i, 0);
         golist[i].transform.localScale = new Vector3(0.05f, 0.05f, 0);
     }
  
 }
 
 // Update is called once per frame
 
 private void fixedUpdate()
 {
     foreach (GameObject starz in golist)
     {
        
         if (starz.transform.position.x > screenBounds.x || -starz.transform.position.y > screenBounds.y
             || starz.transform.position.x < -screenBounds.x || -starz.transform.position.y < -screenBounds.y)
        
         {
             starz.transform.position = gameObject.transform.position;
             aa = Random.Range(0, 2) * 2 - 1; bb = Random.Range(0, 2) * 2 - 1;
            
         }
       
         starz.transform.position += new Vector3((aa + Time.fixedDeltaTime),
                                                 (bb + Time.fixedDeltaTime), 0);
     }
    
 }
   
}
Answer by ShadyProductions · Feb 05, 2020 at 05:19 PM
Take a look at here:
https://www.youtube.com/watch?v=nGw_UBJQPDY
He covers infinite scrolling starfield + parralax effect of stars in background moving slower.
thanx..but i want to create it with a single gameobject in unity 2d.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                