playMaker

Author Topic: 2D collision/trigger without rigidbody2D  (Read 2091 times)

johanesnw

  • Playmaker Newbie
  • *
  • Posts: 26
    • View Profile
2D collision/trigger without rigidbody2D
« on: March 18, 2014, 11:18:07 PM »
You can assume my project is left-right Tower Defense.
so allies can pass thru, but when its in front of enemy it stops and attack

all pics taken from google. just don't want to reveal our game arts.  :P

on my Player Object, I have 2 BoxCollider2D. one to check collision, and one is a trigger (the X size is double) to make enemy stops and attack when it enters the trigger. picture will make it clearer.


on the enemy Object, theres a BoxCollider2D, and it has a sword(child) with PolygonCollider2D(trigger)


then I use Trigger Event on the enemy when it touch player trigger area.


But it wouldn't work without rigidbody2D attached. After I attach rigidbody2D, I think I've accomplished a problem... but when I try to spawn them continuously, this happens.

how could you play Tower Defense if the enemy wave is politely queue-ing to attack  ???
and the performance get really bad. below 20FPS. I've read somewhere that trigger stay will be a performance hit.

so can anyone here help me?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12344
  • Official Playmaker Support
    • View Profile
Re: 2D collision/trigger without rigidbody2D
« Reply #1 on: March 19, 2014, 01:58:33 AM »
Hi,

 I am not too sure what is the problem here.

 basically you can reproduce the same "STAY" effect by listening to Trigger Enter and Trigger Exit and maintain a fsm bool variable "Stay" that ou raise to true on enter and false on exit, then other fsm can simply watch this variable or you can manually forward your own events based on this.

 typically, 5 to 10 object slowing down to 20FPS is a very obvious issues, so I think you should profile this and see what is actually taking to much perfs and work your way in to optimize it. As is I don't see what could be the problem really.

bye,

Jean

johanesnw

  • Playmaker Newbie
  • *
  • Posts: 26
    • View Profile
Re: 2D collision/trigger without rigidbody2D
« Reply #2 on: March 19, 2014, 02:55:18 AM »
I was working with this for hours and finally found the problem.
I have the enemy set to "enemies" layer, but the child is in "default" layer.
After I change both to same layer, I can take 20+ of them in a scene with normal 60-80 FPS.

Then I try to simulate the trigger again. in less than a sec, I got 60+ debug info for onTriggerStay2D. I'll try using STAY as an fsmBool.

another problem.
seems like the onTriggerExit only listens when the object exits a trigger by itself.

I have simulated things..

here's the state. so whenever I move the player, or the player destroys, it won't count as onTriggerExit. or maybe I misunderstans things.  :P

johanesnw

  • Playmaker Newbie
  • *
  • Posts: 26
    • View Profile
Re: 2D collision/trigger without rigidbody2D
« Reply #3 on: March 20, 2014, 04:05:21 AM »
unfortunately not working. even if I use fake "Stay", it continuously checks for onTriggerStay. even after I remove the 2dproxy.
I tried to simulate with 10 enemies and FPS already fall below 5.

but finally I come up with another solution. instead of using collider for trigger, I'm using a vector3 in front of player and update it every frame as the player moves. when the enemy position goes less than the vector3's X position it will begin attacking.
Last time I simulate this, it can maintain 80+FPS with 100 enemies. (I really count it  :P)
as I progress till now it's still a better solution than using trigger stay.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12344
  • Official Playmaker Support
    • View Profile
Re: 2D collision/trigger without rigidbody2D
« Reply #4 on: March 25, 2014, 05:37:02 AM »
Hi,

 Trigger stay is evil, especially within PlayMaker environment, simply for performance reasons. So yeah, custom solutions are sometimes better and more performant.

bye,

 Jean