- Home /
Trouble with deleting a child from main camera.
Hey everyone, I'll try to make this short and sweet. I have this astronaut player, asteroids, and asteroid indicators (flashing arrows that show where an asteroid is approaching from). Every time an asteroid is spawned, an asteroid indicator is spawned as a child of the main camera (so that the indicators move with the player). I have an Asteroid script attached to every asteroid, which checks the distance between itself and the player. When an asteroid comes within a certain distance of the player, I try to delete the first child of the main camera (because the "oldest" child would correlate with the closest asteroid to the player). As of now though, the child on the camera doesn't delete when an asteroid comes within range of the player, any ideas or help is appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Asteroid : MonoBehaviour {
SurvivalController sc;
Rigidbody2D rigid2D;
public GameObject explosionPrefab;
public GameObject asteroidIndic;
public float moveSpeed = 1f;
public float rotateSpeed = 5f;
public float damageToPlayer = 5f;
public bool moveable = true;
private GameObject player; // asteroid tracking
public float minDist;
public float distanceTo;
Vector2 startPos;
private void Start()
{
sc = FindObjectOfType<SurvivalController>();
rigid2D = transform.GetComponent<Rigidbody2D>();
player = GameObject.Find("Player");
startPos = transform.position; // get start position of the spawned asteroid
if (moveable)
{
Instantiate(asteroidIndic, new Vector2(Camera.main.gameObject.transform.position.x + 8f, startPos.y), Quaternion.identity, Camera.main.gameObject.transform);
}
}
void Update ()
{
distanceTo = transform.position.x - player.transform.position.x; // x distance between player and closest asteroid
if (distanceTo == minDist)
{
Destroy(Camera.main.transform.GetChild(0).gameObject); // destroy oldest asteroid indicator, which correlates with closest asteroid
}
if (moveable)
{
rigid2D.velocity = -Vector3.right * moveSpeed; // left movement
}
transform.Rotate(0, 0, rotateSpeed * Time.deltaTime); // rotation
}
Checking the distance to the player is extremely expensive and I would stop using that on every single asteroid.
Use OverlapSphere() https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html to check any asteroids close then start checking the distance.
Thanks, I'll look into OverlapSphere() later tonight :)
could you do a gameobject that you set those indicators to in the UI as well. rather than deleting children you could set them setactive(false);
Your answer
Follow this Question
Related Questions
Get Object after Frustum Culling or Get Object in Camera 0 Answers
How to detect an object which be in FOV of certain camera ? 1 Answer
cannot drag script to player.Guitext error,cannot drag player script to the player in hierarchy 2 Answers
Forcing a GameObject to the highest depth (closest to camera) 2 Answers
How can I check when the centre of camera is looking at a gameobject? 2 Answers