Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mealone · May 26, 2014 at 06:59 AM · collisioninstantiateraycastcombo

help with scripting combo points

hello all, i'm building a 2d game, and i have balloons randomly spawning on the screen which will be popped with the user taps/clicks them. i wanted them to overlap and not collide as they have rigidbody2d and colliders attached and i used this code to ensure that they do not collide :

 function Start () {
     Physics2D.IgnoreLayerCollision(9,10, true);
 }

this works great, and the items overlap instead of colliding. i would like however that when two or more balloons overlap each other and are popped (touched/clicked) at the same time they do a combo and the combo effect prefab is spawned at that point where the multiple balloons were popped.

i have this code added to each balloon prefab (there are four different balloons each with different a different color).

 var pop : GameObject;
 var balloontype : GameObject;
 
 function Update () {    
      if (Input.GetMouseButtonDown(0))
      {
        var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
        if (hitm.collider != null && hitm.collider.gameObject == balloontype)
        {
          Instantiate(burst, hitm.point, Quaternion.identity);
             Instantiate(pop, hitm.point, Quaternion.identity);
          Destroy (hitm.collider.gameObject);
        }
    }
 }

and i would like to add some code which states that when two eyes are hit at the same time by the ray spawn the prefab with the combo animation in that area, and spawn only one, not two combo prefabs (one on each eye).

i cannot seem to figure out how to get this done.... if anyone could help it would be greatly appreciated! thanks in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Midnaut · May 26, 2014 at 07:20 AM

take at look at using this, RaycastAll(), instead of raycast, you will then receive an array (of RayCastHit2D) for all the colliders it hits.

from there you can, first check the length of the array is greater than 1, from there check what types of balloons were hit.

finally if the balloon type is an "eye" more than once inside the array, spawn the combo prefab at the first collision location.

if you want me write out everything for you, just ask.

Comment
Add comment · Show 3 · Share
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 mealone · May 26, 2014 at 02:51 PM 0
Share

Hey thanks... I think I understand the answer... But could you write it out for me please?

avatar image mealone · May 27, 2014 at 02:36 AM 0
Share

Hey, I tried to see if I could get this done but I can't figure out how to do it, can you write it out for me please?

avatar image Midnaut · May 27, 2014 at 07:33 AM 0
Share
 var balloontype : GameObject;

 var pop : GameObject;
 var burst : GameObject;
 
 var firstHit : Vector3 = new Vector3();
 var myHitList : List.<GameObject> = new List.<GameObject>();
  
 function Update () 
 {    
      if (Input.Get$$anonymous$$ouseButtonDown(0))
      {
         //clear the list and reset the hit vector
         myHitList.Clear();
         firstHit = new Vector3();
         
         
          //this one was a mighty head scratcher, make sure you have all your colliders as 2d colliders
            var hits : RaycastHit2D[];
         hits = Physics2D.GetRayIntersectionAll(Camera.main.ScreenPointToRay(Input.mousePosition),100);
         
         for(i = 0; i < hits.Length ; i++)
         {
             Debug.Log("clicked : " + i);
                //comparing the clone to the prefab reference wont work, i personally would use a class that holds types and use get 
             //component, but in this case we will just check the name.
            if (hits != null &&
                 hits[i] != null &&
                 hits[i].collider != null && 
                 hits[i].collider.name.Contains(balloontype.name))
                {
                  myHitList.Add(hits[i].collider.gameObject);
                }
         }
         
         if(myHitList.Count ==  1)
         {
             //if only one hit, then no combo and the "balloon" is destroyed
             Destroy(myHitList[0]);
         }
         else if(myHitList.Count > 1 )
         {
             //if we have multiply hits then lets nuke them and get some pretty action happening
             firstHit = hits[0].point;
             ComboPop(myHitList, firstHit);
         }
         
        }
 }
    
    
 function ComboPop (list : List.<GameObject>, point : Vector3)
 {
    //spawn combo at first collision
        Instantiate(burst, point, Quaternion.identity);
     Instantiate(pop, point, Quaternion.identity);
     
     //clean up balloons
        for(i = 0; i < list.Count ; i++)
        {
         Destroy (list[i]);
     }
 }


notes: make a prefab for the balloon, and effects, this runs by name but would be best if running off something like a class, also make sure your colliders are all 2D colliders. This is also designed to be attached to the main camera, and i kept it to a bare $$anonymous$$imum so you could extend a bit.

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

21 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

Related Questions

Custom Collision Detection 4 Answers

physics2d.raycastall help please 1 Answer

Play Audio-Clip On Ray Casting Collision 1 Answer

Have falling object exit from a collider after collision? 2 Answers

OnCollisionEnter not being called, between a Rigidbody and a Box Collider 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