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*.transform.position = new Vector3(0.02f + i, 0.02f + i, 0);*
golist*.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);
}
}
}