2D Toolkit Forum
2D Toolkit => Support => Topic started by: mukarillo 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:
mySprite.CurrentSprite.customColliders[0]
But i don't know how to compare their both.
Can someone help me with that?
-
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:
(http://s10.postimg.org/flmml0hs9/example.png)
Sorry for double posting.
Please someone help me :(
Thanks!
-
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.
-
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:
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:
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:
(http://s30.postimg.org/ibiti52pt/Screen_Shot_2015_05_23_at_10_44_19_AM.png)
I guess i'm not getting this math right :-\
-
Im sorry for double posting again, but i figured it out how to get the collider in a rect:
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 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:
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:
(http://s24.postimg.org/g3batprqd/hitbothsides.png)
I made a DrawRect to see in inspector where were the lines, here's the code piece:
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