Unity 2D Spawn Infinity Star

VIDEO PART NUMBER ONE

Part number 1 Spawn Infinity Stars for Mobile and Desktop.

VIDEO PART NUMBER TWO

Part number 2 Spawn Infinity Stars for Mobile and Desktop.

C# CODE

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnStar : MonoBehaviour
{
public GameObject starObj;
public int maxStars = 100;
// Start is called before the first frame update
void Start()
{
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));
for(int i = 0; i < maxStars; ++i)
{
GameObject star = (GameObject)Instantiate(starObj);
star.transform.position = new Vector2(Random.Range(min.x, max.x), Random.Range(min.y, 40));
star.GetComponent().speed = -(1f * Random.value + 0.5f);
star.transform.parent = transform;
}
}

// Update is called once per frame
void Update()
{

}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StarController1 : MonoBehaviour
{
public float speed = -0.2f;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
Vector2 pos = transform.position;
pos = new Vector2(pos.x, pos.y + speed * Time.deltaTime);
transform.position = pos;
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));
if(transform.position.y < min.y)
{
transform.position = new Vector2(Random.Range(min.x, max.x), max.y);
}
}
}

Is this Articolo useful? 0 of 0 people say this Articolo is useful.
- / 5
Grazie per aver votato!