- Home /
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. 
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");
}
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.
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.
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.
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?
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;
}
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.
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);
}
Your answer