0
Answered

Companion behavior, seeing run-time generated enemies in the enemy layer.

VictorSB 5 months ago in Behavior updated 5 months ago 3

I am creating a complex behavior for my companion that fires at enemies as they appear. My companion is a character with a behavior component and a perception component that includes two checks on the sight layer mask, the player layer and the enemies layer.

I have no issues having my companion following the player but I don't know how to make the condition of my companion seeing runtime generated enemies. I have tried for it to have the condition with a GameObject prefab of those enemies but it does not seem to work since the conditions is never matched.

Am I doing something wrong?

Thanks

Unity version:
2019.3.7f1
Game Creator version:
1.1.6

Actually I have to update,

The Can See condition in behavior nodes seems to be able to work only from player to invoker and viceversa, any other thing that I select specially a game object will never trigger the condition.

I modified the example 1 where the character raises its arms when seeing the player. I placed a cube on my scene and make it a prefab, I then changed the condition in the character behavior to can see game object and drag the cube prefab.

I then placed the prefab clearly in view of the character but it did not trigger the raise arms state.

Some screenshots

Answered

Hi Victor!

I think I see the problem. The thing is that Scriptable Objects shouldn't be able to access scene objects. Either you've dragged the prefab cube onto the Can See "target" field or Unity has a bug and allowed you to do that. If it's the second case, if you restart Unity, the value of that field should have disappeared (Unity can't serialize scene object references onto scriptableobjects).

To solve this problem we created the Blackboard, which is at the top-left corner of the Behavior Graph window. If you open that up, you should be able to create a variable that allows to communicate the behavior graph with scene objects. Create a new Game Object entry and set it to Game Object. Give it a name like "enemy-target".

Now, in the project, if you select your companion, you'll see a new field called "Enemy Target". You'll need to have a separate action that sets the target. Let's do this first: Create A Trigger set to On Start and call an Actions. You want to look for objects nearby the companion. You can use the "Gather Components by Distance" action, which fills a List Variable with the objects surrounding the companion based on distance. The next action is a Assign Game Object so that the index of the List Variables's 1st value is set to the "Enemy Target" field.

To do this, set the target as a Local Variable and the companion as the target. You'll see there's a Local Variable called "Enemy Target". Lastly, use a Wait 

If you got lost here, take a look at the Livestream we did with The Messy Coder. Something similar was done around the 01:04 timestamp: https://www.twitch.tv/videos/599228541.

Now that the "Enemy-Target" field has a game object with the closest enemy, you can access the "Enemy Target" value from the Behavior Graph using a Local Variable and setting the source as the Invoker. Type in the name of the "enemy-target" (as you spelled it in the Blackboard) and you'll get the value. For more info on using the Blackboard, see the documentation: https://docs.gamecreator.io/behavior/behavior/behavior-graph/blackboard.

Hope this helps! The upcoming tutorials will cover this in more detail (a companion and an enemy share many mechanics).

+1

Wonderful explanation and indeed it worked perfectly! thanks!