playMaker

Author Topic: Best way to accomplish this NPC movement?[SOLVED]  (Read 4528 times)

mphilips

  • Playmaker Newbie
  • *
  • Posts: 8
Best way to accomplish this NPC movement?[SOLVED]
« on: March 04, 2014, 01:57:04 AM »
Still new to Unity & Playmaker so bear with me.

I'm trying to find the most efficient method of moving NPCs, detecting their collision between friend & enemy and attacking when they collide. All in a single line. (So 5 friendly units would constantly collide with each other in a line & same with enemies).

Currently I've set up rigidbody2D and 2D box colliders on a player and enemy prefab. For testing, I made 2 buttons. 1 is for player spawning and 1 is for enemy spawning. Click the button and it creates a clone of the prefab which starts walking towards enemy base by setting velocity 2D.

Enemy & Player units would most likely collide in the middle and fight. Units in the back might do nothing but move until they are in range to fight.

I also am currently planning on using 3-4 FSM in each prefab. (So 5 player units would = 15-20 FSM at once). 1 for stat set up/setting graphics etc.. 1 for collision, 1 for combat & possibly 1 for playing animations.

This all works fine, however the moment I spawn 6+ enemies I start slowing down to a crawl in the editor and get 500ms 99% physics2d.callbacks in fixedupdate.

Funnily enough, I am designing this for mobile and it actually runs totally fine on my Galaxy Nexus even with 25+ spawns colliding with each other constantly.

I was planning to use raycasts to detect collisions etc.. but haven't made it that far due to this performance issue.

Should I be relying on physics and colliders in this case? The only other idea I had was to manually get nearest enemy/player game object target and constantly store the distance until it is within a range (no raycast) and go from there.

Not sure if this is specific to playmaker or not but any advice would be great.

Thanks

« Last Edit: July 17, 2014, 09:18:40 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Best way to accomplish this NPC movement?
« Reply #1 on: March 04, 2014, 07:26:52 AM »
Hi,

 the editor performances should never be relying upeon because a lot of the editors windows are using what's happening and they take a lot of performances out. so if it runs fine on your mobile, then this is good. Have you ran a profiler from the scene running on the mobile what does it says?

you should always go easy on raycasting. I believe triggers will be more effective, also, avoid completly using "STAY" collisions and triggers prefer watching for enter and exit and maintain a boolean flag "stay" manually, this is better ( in my opinion).

bye,

 Jean

mphilips

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Best way to accomplish this NPC movement?
« Reply #2 on: March 04, 2014, 03:02:15 PM »
Thanks for the advice Jean. I looked into the profiler a bit more and it looks like the console is being spammed with thousands of this per character:

OnCollisionStay2D False
UnityEngine.Debug:Log(Object, Object)
PlayMakerUnity2DProxy:OnCollisionStay2D(Collision2D) (at Assets/PlayMaker Unity 2D/Components/PlayMakerUnity2DProxy.cs:131)

and that is where the profiler is saying callbacks are coming from too with LogStringToConsole at 96% of usage.

So, do I have debug mode enabled somewhere that is causing these logs to send to console?

Either way, I'll rework this to follow your advice, just good to know for the future.

Thanks,
Mike

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Best way to accomplish this NPC movement?
« Reply #3 on: March 05, 2014, 02:44:44 AM »
hi,

 that script has a public debug bool variable, turn it off and it will stop outputing to the console.

bye,

 Jean

Moosegun

  • Playmaker Newbie
  • *
  • Posts: 7
Re: Best way to accomplish this NPC movement?
« Reply #4 on: June 03, 2014, 12:19:31 PM »
Sorry to dig up an old thread but was going to post on this issue but found this via search and thought my feedback might be useful.  I had a massive issue with fps drop and lag when testing within unity, and even outside unity with profiler.  Narrowed the issue down to this.  Switching off the bool turned scenes which were dropping to 2fps into a steady 90fps!

Means that I can test within unity again, which is vital to find many bugs.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Best way to accomplish this NPC movement?
« Reply #5 on: July 16, 2014, 08:02:56 AM »
Hi,

 Have you been able to make some progress on this?

 Bye,

 Jean

mphilips

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Best way to accomplish this NPC movement?
« Reply #6 on: July 16, 2014, 04:59:03 PM »
Hi Jean,

Turned out it was the logging issue you mentioned. Sorry I didn't mention sooner. Thanks!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Best way to accomplish this NPC movement?[SOLVED]
« Reply #7 on: July 17, 2014, 09:19:24 AM »
Hi,
 
no worries, going through my back log as much as I can to make sure I don't forget posts I was involved in or posts that did not get attention ( to anyone reading this, bump your thread :))

 Bye,

 Jean