playMaker

Author Topic: Check and store all enenies in front of player  (Read 3304 times)

JOY

  • Junior Playmaker
  • **
  • Posts: 51
  • From JOY Entertainment
    • JOY Entertainment
Check and store all enenies in front of player
« on: December 06, 2016, 11:05:42 AM »
Hi,

I'm very new with Unity and Playmaker and couldn't find the solution in the search result.
I need to detect all enemies in front off the player and store as an object array for other actions. This is a running game with 3 lands.
I tried to use Raycast to cast in front of the player (distance 5) with Transform Direction (0,0,1) but I got some problems:
 - Raycast only detects and store first object. I tried both Raycast and Raycastall but was not successul
 - I cannot manage to detect enemy in other land. I think I should add an FOV from the player but I don't know how to achieve that
I'd like to ask whether this solution is fine for mobile games since the game needs to send the raycast every frame
« Last Edit: December 07, 2016, 03:51:14 AM by JOY »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Check and store all enenies in front of player
« Reply #1 on: December 06, 2016, 02:17:53 PM »
Hi,
Raycast all should work.
When you turn on debug on the action you can see the Ray it's casting (standard a yellow line)

For the direction you can play around with the direction, it is best to test that in run-time so you can see the line.

You can also work with layers, add a layer mask and chose the element. and give the objects that layer

When you cast every frame, when the ray is not hitting an enemy anymore it will also be removed from the array. But the last one hit will stay in the array.

If you have multiple raycasts connected to 1 array it will only get the result from 1 raycast.
So you need a separate array for each cast and then merge the arrays when needed.

JOY

  • Junior Playmaker
  • **
  • Posts: 51
  • From JOY Entertainment
    • JOY Entertainment
Re: Check and store all enenies in front of player
« Reply #2 on: December 06, 2016, 09:05:44 PM »
Hi djaydino,

Thank you for your support.
The debug line only show one direction while I'd like to have an angle (Ex: only cast 135 degrees in front of the player)
I already used layer.
So I think the logic should be using Raycast instead of Raycastall. When the ray hits the first enemy, I store it in the array. Then I moved that enemy to Ignore Raycast layer so that the next ray won't hit it. Is it correct?

Update:
I also tried to use SphereCast but it seems check behind the player as well and I cannot cast to all objects.

Thanks.
« Last Edit: December 07, 2016, 03:00:29 AM by JOY »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Check and store all enenies in front of player
« Reply #3 on: December 07, 2016, 09:58:29 AM »
Hi,
Raycast only sends a single line, so you would need at least 3 raycasts

Maybe you could use a trigger instead : make an empty object and add a collider, edit and resize it so it will detect each lane and place (as a child) in front of the player.
Give the enemy a trigger, so when triggering it will add to the array.

You can also add another trigger behind the player and when enemy triggers that it can remove from the array

JOY

  • Junior Playmaker
  • **
  • Posts: 51
  • From JOY Entertainment
    • JOY Entertainment
Re: Check and store all enenies in front of player
« Reply #4 on: December 07, 2016, 10:04:43 PM »
Yes, you are right. I'm trying with the trigger at the moment and got stuck with removing enemies from the array and you open my eyes. I plan to use only 1 trigger but it's sphere collider so while I need to remove them right after the player passes them.
Thank you so much.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Check and store all enenies in front of player
« Reply #5 on: December 08, 2016, 05:17:18 AM »
Happy to help