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 Centrixed · Jan 22, 2016 at 04:39 PM · scripting problemlearningobject referenceclass object

Trying to track the BoxColliders of 2 Different Sets of Instantiated GameObjects

Basically I have dynamically spawning (instantiated) "enemies" that will each have their own BoxCollider2D's. I also have instantiated projectiles coming from my player with their own unique BoxCollider2D's as well. I have been trying to think of a way to dynamically track these colliders, since they have no unique properties to track individually. Is it possible to group them? For example: "if a collider from the group of projectile boxcolliders touches a collider from the group of enemy colliders ..etc". I am still learning the in's and out's of Unity Engine, so I may just be overlooking a tool they already provided. Can someone please give me insight on how to accomplish this, or at least where to start? Thanks! (Also if this code has a horrible flaw, please point it out so I can fix my mistake)

Here is my code for the projectile (animations are handled in another script):

 public class Projectile : MonoBehaviour {
 
     public GameObject projectilePrefab;
     public float speed = 0f;
     private Rigidbody2D playerRigid;
     private Rigidbody2D cloneRigid;
     private GameObject clone;
     private MovementScript playerMove;
     private Animator animate;
     private Vector2 projectileSpeed;
     private bool shot = false;
 
     void Start () {
         playerRigid = GetComponent<Rigidbody2D>();
         playerMove = GetComponent<MovementScript>();
         animate = GetComponent<Animator>();
     }
     
     void Update () {
         //If Player is facing right, adjust projectile direction on x axis.
         if (playerMove.faceDirection) {
             projectileSpeed = new Vector2(1 * speed, 0);
         }
         //If Player is facing left, reverse projectile direction on x axis.
         else if (!playerMove.faceDirection){
             projectileSpeed = new Vector2(-1 * speed, 0);
         }
        //isThrowing is handled in another script. It basically handles the cooldown time between throws.
         if (playerMove.isThrowing) {
             if (!shot) {
                 shoot();
                 shot = true;
             }
         }
         else if (!playerMove.isThrowing) {
             shot = false;
         }
     }
 
     //Instantiates the object using the projectile prefab, then rotates/scales it accordingly and projects it outwards.
     void shoot() {
         //Instantiates the projectile, and gets components of the clone to work with.
         clone = (GameObject)Instantiate(projectilePrefab, playerRigid.position, Quaternion.identity);
         cloneRigid = clone.GetComponent<Rigidbody2D>();
         Transform cloneTrans = clone.GetComponent<Transform>();
 
         //Rotates the sprite so it visually flies in the correct direction
         if (playerMove.faceDirection) {
             cloneTrans.Rotate(0, 0, 270);
         }
         else {
             cloneTrans.Rotate(0, 0, 90);
         }
         //Scales the sprite to a feasable size
         cloneTrans.localScale = new Vector3(15, 13, 1);
 
         //Applies velocity and destroy clone after 3.5 seconds
         cloneRigid.velocity = projectileSpeed;
         Destroy(clone, 3.5f);
     }
 }
Comment
Add comment
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to create a repository of enemy objects that are initialised but then get a new instance of a random one? 1 Answer

unity mobile app with learning locker or LRS 0 Answers

Light Flickering Problem - Wrong objects get referenced by the script 0 Answers

For some reason Object reference not set to instance of an object 2 Answers

Object reference not set to an instance of an object 1 Answer


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