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
1
Question by BillyM148 · Jul 31, 2013 at 03:33 AM · collisioncollidersontriggerexit

I'm making a little card game and I'm having trouble with collisions, or maybe just the overall logic.

As you can see, some cards are beneath others. alt text

My cards have a front and back, they're just planes. Then i have 5 children of each card. 4 of the children are empty game objects with box colliders attached that i placed at each corner (up, down, left, right). This is to know if a card that is face up, is on top of a card that's face down. For example, the 8 of diamonds is on top and should be colliding with those 2 cards that are face down to the left and right. When I click the 8 a diamonds it's position moves away and the colliders attached to the back of it and also the colliders attached to the backs of the 2 cards beneath the 8 a diamonds, should no longer be colliding and which in turn, those 2 face down cards should now be rotated to be facing up.

OnTriggerEnter is working but OnTriggerExit is never called. I can't figure out why. Or if anybody else has a different logic to go with when to know when a card is no longer on top of another, i'm open for ideas.

 if (Input.GetMouseButtonUp(0))
         {
             //print(transform.childCount);
             var hit: RaycastHit;
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, hit))
             {
                 object = hit.transform.gameObject;
                 //print(object);
                 transform.position = Vector3(15, 15, 0);
                 //Destroy(object);
             }
         }
 
 function OnTriggerEnter (collisionInfo : Collider) 
 {    
     Debug.Log("Enter");    
     Debug.Log(transform.parent.gameObject.name + " Colliding with " + collisionInfo.transform.name);
 }
 
 function OnTriggerExit (other : Collider) 
 {
         transform.parent.rotation.eulerAngles.y = 180;
         Debug.Log("Exit");
 }
card111.png (215.8 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by highpockets · Jul 31, 2013 at 04:02 AM

-You could use a Physics.Ratcast that shoots down from the corners of the card that goes away and then shoot more ray casts up from the corners of the cards that were hit and if they don't hit, they should flip.

Comment
Add comment · 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
0

Answer by robertbu · Jul 31, 2013 at 04:16 AM

I played for a few minutes trying to duplicate your OnTriggerEnter() without getting an OnTriggerExit(). I tried various combinations of Rigidbodies, colliders, and "teleporting" object. I could not make it happen, so I'm sure what is going on.

But here is an alternate solution. Just do a short Raycast() up from the corner of each card. It would not require child game objects, nor a Rigidbody on the cards. I think even on mobile you would be fine with that many raycasts, especially since you are raycasting against box colliders. If performance is an issue, you could reduce the number of raycasts you do per second. Five or ten times a second would do, and you could stagger the times to keep the load even.

Another possibility would record the level of each card as it is dealt. The each card could check itself against any cards in the level above. If the distance between the center of the below card to the center of the one above falls below a certain threshold, then the card is still covered. If no cards are found within that distance, then the card is uncovered and it can be flipped.

Another solution is to build a data structure and just read the info from your data structure.

Comment
Add comment · Show 10 · 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 BillyM148 · Jul 31, 2013 at 04:36 AM 0
Share

Thx for the replies guys. I originally tried a version of raycasting but it wasn't working out for me. It's probably because I'm not exactly an advanced programmer. I'm still learning. I'm not sure if I'll be able to exercise your ideas.

avatar image robertbu · Jul 31, 2013 at 05:19 AM 0
Share

Are you using built-in planes or something else? What is the orientation of your cards (i.e. are they on the XY plane or the XZ plane?

avatar image robertbu · Jul 31, 2013 at 05:48 AM 0
Share

Here is a bit of code that implements the raycast idea. Note it assumes the camera is looking towards positive 'Z'.

 #pragma strict
 
 var size = 1.0;   // Size of the plane taking into account
                   //   changes in scale.  The built-in plane
                   //   is 10 x 10 before any scaling
                   
 var speed = 180.0;  // Speed the card is flipped 
                     //   (degrees per second)
 
 private var flipped = false;
 private var qTo = Quaternion.identity;
 private var offsets : Vector3[] = new Vector3[4]; 
 
 function Start () {
     var horz = size * 0.5;
     var vert = size * 0.5;
     offsets[0] = Vector3( horz,  vert, 0.0);
     offsets[1] = Vector3( horz, -vert, 0.0);
     offsets[2] = Vector3(-horz, -vert, 0.0);
     offsets[3] = Vector3(-horz,  vert, 0.0);
 }
 
 function Update () {
     transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
     if (!flipped && !IsCovered()) {
         flipped = true;
         qTo = Quaternion.Euler(0.0, 180.0, 0.0);
     }
 }
 
 function IsCovered() : boolean {
     for (var i = 0; i < offsets.Length; i++) {
             Debug.DrawRay(transform.position + offsets[i], Vector3.back * 3.0);
         if (Physics.Raycast(transform.position + offsets[i], Vector3.back, 0.1))
             return true;
     }
     return false;
 }
avatar image BillyM148 · Jul 31, 2013 at 06:00 AM 0
Share

First of all, thank you so much, robertbu, for taking up your own time to help out a total stranger. People like you are awesome...much respect to you! Once I get back to my computer, I'll test it out/play with it and let ya know how it goes. I've spent over a week trying to figure this out.

Thanks.

avatar image BillyM148 · Jul 31, 2013 at 09:35 AM 0
Share

I'm not sure what's going on in the start function. As I said, I'm sort of learning and not very advanced. Can you offer some insight?

 function Start () {
     var horz = size * 0.5 * transform.localScale.x;
     var vert = size * 0.5 * transform.localScale.z;
     offsets[0] = Vector3( horz,  vert, 1.0);
     offsets[1] = Vector3( horz, -vert, -10.0);
     offsets[2] = Vector3(-horz, -vert, 2.0);
     offsets[3] = Vector3(-horz,  vert, 3.0);
 }
Show more comments

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

16 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

Related Questions

Can two different composite colliders collide? 0 Answers

How to detect if target is inside an "arc" area? 0 Answers

Level colliders reducing acceleration 0 Answers

OnMouseDrag, object getting dragged goes through collider instead of stopping 0 Answers

Why does my player fly through my barrier? 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