Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TysonG · Apr 25, 2019 at 09:41 PM · camerascripting problemgameobjectchild objectdelete

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
     }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jmgek · Apr 25, 2019 at 09:52 PM 0
Share

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.

avatar image TysonG jmgek · Apr 26, 2019 at 12:18 AM 0
Share

Thanks, I'll look into OverlapSphere() later tonight :)

avatar image surfuay · Apr 26, 2019 at 12:19 AM 0
Share

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);

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

248 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges