About the gameobject and main camera

why my asteroid is still in the main camera even though I’ve moved the gameobject

I can’t be certain from the image what I’m seeing.
What do you expect to happen when play begins?
What is happening?
Paste in your AsteroidSpawner code (the text of the script, not a screenshot).

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

public class AsteroidRandomFlight : MonoBehaviour
{
  [SerializeField] private GameObject[] PrefabsCollect;
  [SerializeField] private float SecondsBetweenAsteroids = 1.5f;
  [SerializeField] private Vector2 forceRange;

  private Camera mainCamera;
  private float timer;

  private void Start()
  {
     mainCamera = Camera.main;  
  }

  private void Update()
  {
      timer -= Time.deltaTime;
      SpawnerAsteroid();  
      timer += SecondsBetweenAsteroids;
  }

   void SpawnerAsteroid()
   {
        int side = Random.Range(0,4);
        Vector2 SpawnPointer = Vector2.zero;
        Vector2 direction = Vector2.zero;
        switch(side)
        {
            case 0:
            //left
                SpawnPointer.x = 0;
                SpawnPointer.y = Random.value;
                direction = new Vector2(1f, Random.Range(-1f, 1f));
                break;
            case 1:
            //right
                SpawnPointer.x = 1;
                SpawnPointer.y = Random.value;
                direction = new Vector2(-1f, Random.Range(-1f, 1f));
                break;
            case 2:
            //bottom
                SpawnPointer.x = Random.value;
                SpawnPointer.y = 0;
                direction = new Vector2(Random.Range(-1f,1f), 1f);
                break;
            case 3:
            //top
                SpawnPointer.x = Random.value;
                SpawnPointer.y = 1;
                direction = new Vector2(Random.Range(-1f, 1f), -1f);
                break;  
        }
       Vector3 WorldSpawnPointer = mainCamera.ViewportToWorldPoint(SpawnPointer);
       GameObject SelectedAsteroid = PrefabsCollect[Random.Range(0, PrefabsCollect.Length)];
      GameObject AsteroidInstance = Instantiate(SelectedAsteroid, WorldSpawnPointer, Quaternion.Euler(0f,0f,Random.Range(0f, 360f)));
      Rigidbody rb = AsteroidInstance.GetComponent<Rigidbody>();
      rb.velocity = direction.normalized * Random.Range(forceRange.x, forceRange.y);
   }
}

I’m still uncertain as to what is happening that is the issue. Please describe what is happening. I’m not sure what you mean by “in the main camera”.


my asteroid prefabs still in the my main camera sir, and I think it’s because using ViewportToWorldPoint(), but when I watched the tutorial it’s no problem about that so what is the solution about the issue

ViewportToWorldPoint is the correct method. I sill don’t understand what you mean by in the main camera. Do you mean on the same plane as the camera?

I notice that your player’s Z is -4.67… is your Camera at 0,0,0? It’s better to bring the camera back (in my project, it’s at 0,0,-10 and the player starts at 0,0,0. If your camera’s at 0,0,0, then the Asteroids will be spawned on the same plane as the camera and not visible (nor will they collide with the player if the player is at z=-4)


this sir, i mean the prefabs is parallel to the main camera even though the asteroid game object is in the same position as the player, but the red and blue asteroid objects are attached to the main camera so that the player cannot collide with meteors.

According to your screenshot, nothing is attached to the camera. Both the blue and red asteroids are at the root level of the scene.
Post a screenshot of the camera’s inspector, and then a screenshot of one of the astroid’s inspector. We’re looking to see what the actual Z value is in the transform.



This sir it’s my position my camera and the game object asteroid, it is different position but the red and blue asteroid of the child asteroidPrefabsgameobject it’s merges :frowning: , maybe the problem from my computer or other think

I was meaning the location of the spawned asteroids when the game is running.
It looks like your camera is in the right place. What is the Z axis on the Blue and Red asteroid when they spawn (while the game is running)?


my asteroid the position is 0,0,0 when not play and when play, but why the asteroid still same position my main camera, maybe I will repeat the game hemmm,

Privacy & Terms