Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to make randomly created objects follow the camera ?

Discussion in '2D' started by spinmaster99, Apr 18, 2019.

  1. spinmaster99

    spinmaster99

    Joined:
    Apr 13, 2019
    Posts:
    1
    Hi everyone me and my friend are working on this game for like 1 week now. The concept is basically a ball falling down (and camera following the ball) while it's trying to evade the piked platforms passing through the screen on its way down . We wrote this code for the piked platform for its movement and vanishing when it is out of the main camera and the platform is a prefab :
    (in the code, "dikenli" means piked)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class dikenli : MonoBehaviour
    6. {
    7.     public float speed = 10.0f;
    8.     private Rigidbody2D rb;
    9.     private Vector2 screenBounds;
    10.    
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         rb = this.GetComponent<Rigidbody2D>();
    15.         rb.velocity = new Vector2(speed, 0);
    16.         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    17.  
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (transform.position.x > screenBounds.x * 2)
    24.             Destroy(this.gameObject);
    25.     }
    26. }
    27.  
    [ICODE]
    [/ICODE]


    And we wrote this code for creating the random piked platforms based on the piked platform prefab and inserted this code to main camera :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class deployDikenli : MonoBehaviour
    7. {
    8.     public GameObject dikenliPrefab;
    9.     public float respawnTime = 1.0f;
    10.     private Vector2 screenBounds;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width , Screen.height , Camera.main.transform.position.z));
    15.         StartCoroutine(dikenliWave());
    16.     }
    17.     private void spawnEnemy()
    18.     {
    19.         GameObject a = Instantiate(dikenliPrefab) as GameObject;
    20.         a.transform.position = new Vector2(screenBounds.x * -2, Random.Range(-screenBounds.y , screenBounds.y));
    21.     }
    22.     IEnumerator dikenliWave()
    23.     {
    24.         while (true)
    25.         {
    26.             yield return new WaitForSeconds(respawnTime);
    27.             spawnEnemy();
    28.          }  
    29.      }  
    30. }          
    31.    
    When the game starts main prefab starts to pass the screen and generates a clon when it is destroyed. The cycle continues. But the piked platform clones stay generated in the area of initial position of the camera and don't follow the main camera. We want the generated clones to follow the main camera and we want their generation area to follow the main camera. How can we accomplish this ? Thanks in advance.

    Here's some pictures of what it looks like :


    The main prefab passing the screen
    Ekran Alıntasdasdasdasısı.PNG


    Randomly generated clones staying in the initial camera area
    asdasdasd.PNG


    To addition to all this i want to ask if there is any way to determine the distance between randomly generated objects so they can generate in specific area (or the area that follows the camera) only with the condition of previously determined distance intervals. Thanks again.
     
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    172
    The camera is a gameObject, you can simply get it's position and add it in the Spawnfunction where you set the position of the prefab. Get a reference like so:
    [SerializeField]
    private Transform cameraTransform;

    Drag the camera object onto it in the inspector. Now you can use cameraTransform.position in your script and add that to the spwaned prefab. You could also just move the whole spawner Object based on the camera position.

    To get the distance between two prefabs you can use:
    float distance = Vector2.Distance(prefab1.transform.position, prefab2.transform.position);