Hello Guest

Author Topic: Detecting collision between 2 Advanced Collider  (Read 4833 times)

mukarillo

  • Newbie
  • *
  • Posts: 16
    • View Profile
Detecting collision between 2 Advanced Collider
« on: May 22, 2015, 02:32:42 am »
Hello there,

I'm making a fighting game and i need multiple boxes colliders in a single sprite, and the Advanced Collider functionality is perfect for it. But i don't know how to check if my hurtbox "overlaps" the hitbox.

I already tried to use this:
Code: [Select]
mySprite.CurrentSprite.customColliders[0]
But i don't know how to compare their both.

Can someone help me with that?

mukarillo

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Detecting collision between 2 Advanced Collider
« Reply #1 on: May 23, 2015, 03:39:17 am »
Well, i've being trying to figure it out by my self, but i didn't went so far. I tried to make a collision based in overlaping, but i didn't manage to get the "advanced collision box" position right.

This is an example of what i'm getting:


Sorry for double posting.

Please someone help me :(
Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Detecting collision between 2 Advanced Collider
« Reply #2 on: May 23, 2015, 01:22:53 pm »
I don't know how you're positioning them but it looks like your dimensions and offsets are off by a constant - are you scaling one thing and not the other?

Re: comparing colliders, its up to you, we just provide the editor and data for you to do what you wish - you can get the collider bounds and manually test using customColliders, or you can create normal unity colliders and use Unity collision using static colliders & Physics2D.Overlap* functions.

mukarillo

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Detecting collision between 2 Advanced Collider
« Reply #3 on: May 23, 2015, 02:28:05 pm »
Hey unikron, thanks for the reply!

I found out that my character was too big, so I scaled it down to 0.6f in x,y and z.
This is how i'm drawing the rects in the screen:
Code: [Select]
for(int i = 0; i< currentSprite.CurrentSprite.customColliders.Length;i++){
DrawRect(new Rect(
gameObject.transform.position.x-(((currentSprite.CurrentSprite.customColliders[i].Size.x)*gameObject.transform.localScale.x)*gameObject.transform.localScale.x),
gameObject.transform.position.y,
(currentSprite.CurrentSprite.customColliders[i].Size.x)*gameObject.transform.localScale.x,
(currentSprite.CurrentSprite.customColliders[i].Size.y)*gameObject.transform.localScale.y),
       Color.blue);
}

I tried many variations and this only works for the HitBox, the HurtBox stays in the character feet.
Can you help me with the math? I mean, if i get the rect right, i can use it to check the collision

Thanks,
Murillo

Edit:

I decided to make a test and I scaled up my character to 1.0f in x,y and z

and this is what i'm trying to do:
Code: [Select]
for(int i = 0; i< currentSprite.CurrentSprite.customColliders.Length;i++){
DrawRect(new Rect(
gameObject.transform.position.x+currentSprite.CurrentSprite.customColliders[i].origin.x,
gameObject.transform.position.y+currentSprite.CurrentSprite.customColliders[i].origin.y,
                currentSprite.CurrentSprite.customColliders[i].Size.x,
currentSprite.CurrentSprite.customColliders[i].Size.y),
Color.blue);
}
And the box isn't aligned with the character:


I guess i'm not getting this math right  :-\
« Last Edit: May 23, 2015, 02:48:06 pm by mukarillo »

mukarillo

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Detecting collision between 2 Advanced Collider
« Reply #4 on: May 23, 2015, 04:06:40 pm »
Im sorry for double posting again, but i figured it out how to get the collider in a rect:

Code: [Select]
new Rect(
(gameObject.transform.localPosition.x+currentSprite.CurrentSprite.customColliders[i].origin.x)-currentSprite.CurrentSprite.customColliders[i].Size.x/2,
(gameObject.transform.localPosition.y+currentSprite.CurrentSprite.customColliders[i].origin.y)-currentSprite.CurrentSprite.customColliders[i].Size.y/2,
currentSprite.CurrentSprite.customColliders[i].Size.x,
currentSprite.CurrentSprite.customColliders[i].Size.y);

Now all i got to do is to
Code: [Select]
Rect.Overlap and manage the collider names.

Thanks Unikron! your asset rocks! ;D

See ya!

Edit:

Haha, sorry for bothering so much, but now i've found another problem.
My character faces the opponent, so when the x of the opponent is higher than the characters x I set mySprite.FlipX to false, and when it's lower, i set to true.
But the Advanced Collision Boxes doesn't follow this rule. They're all in the same place, even if the sprite is flipped.
Can you help me with that?

THANKS!

Edit 2:

Well, i've managed to make that. If anyone faces this problem, here's how to solve it:

Code: [Select]
float originX = (currentSprite.FlipX) ? -currentSprite.CurrentSprite.customColliders[i].origin.x : currentSprite.CurrentSprite.customColliders[i].origin.x;
Rect collisionRect = new Rect(
(gameObject.transform.localPosition.x+originX)-currentSprite.CurrentSprite.customColliders[i].Size.x/2,
(gameObject.transform.localPosition.y+currentSprite.CurrentSprite.customColliders[i].origin.y)-currentSprite.CurrentSprite.customColliders[i].Size.y/2,
currentSprite.CurrentSprite.customColliders[i].Size.x,
currentSprite.CurrentSprite.customColliders[i].Size.y);
Here is it working:


I made a DrawRect to see in inspector where were the lines, here's the code piece:

Code: [Select]
void DrawRect(Rect r, Color c){
Debug.DrawLine(new Vector3(r.x, r.y, 0), new Vector3(r.x +r.width,r.y,0),c);
Debug.DrawLine(new Vector3(r.x+r.width, r.y,0), new Vector3(r.x+r.width,r.y+r.height,0),c);
Debug.DrawLine(new Vector3(r.x+r.width, r.y + r.height, 0), new Vector3(r.x, r.y+r.height,0),c);
Debug.DrawLine(new Vector3(r.x, r.y+r.height, 0), new Vector3(r.x, r.y, 0),c);
}

See ya  :D
« Last Edit: May 23, 2015, 07:53:08 pm by mukarillo »