Answers for "how to make point spawn random on screen after collected"

C#
0

how to make point spawn random on screen after collected

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

public class Collect : MonoBehaviour
{
    private Rigidbody2D rb;
	//this goes on player as a script
    [SerializeField]
    private GameObject Point;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        SpawnPoint();
    }

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

    private void FixedUpdate()
    {
    }

    private void SpawnPoint()
    {
        bool pointSpawned = false;
        while (!pointSpawned)
        {
        	//replace the range with points on your camera, first is X cord second is Y cord
            Vector3 pointPosition =  new Vector3(Random.Range(-13f, 13f), Random.Range(-8f, 8f), 0f);
            if ((pointPosition - transform.position).magnitude <  3)
            {
                continue;     
			}
            else
            {
                Instantiate(Point, pointPosition, Quaternion.identity);
                pointSpawned = true;
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Destroy(collision.gameObject);
        SpawnPoint();
    }
}
Posted by: Guest on July-22-2020

Code answers related to "how to make point spawn random on screen after collected"

C# Answers by Framework

Browse Popular Code Answers by Language