0
Answered

Player controls AI's target

Crispy 4 months ago in Game Creator updated by Marti (Lead Developer) 4 months ago 1

I want to have a feature in my game where the Player auto-attacks, and the user can press a button to switch targets so that the Player will cycle through all nearby targets that are within a certain radius IN FRONT of the Player -- kind of like the Can See condition. I think I should use a List Variable with the Nearest with Tag Action and the lock-on Action in the coming Shooter Module, but I don't know what other Actions I can use to achieve this.

Unity version:
Game Creator version:
1.0.1

Answer

Answer
Answered

Excellent question. You're correct in using the Gather Action. Here's how I would do it:

Disclaimer: While preparing this example we noticed some missing features in version 1.0.1. This will be added in 1.0.2, which should be available tomorrow.

First of all, we need a Trigger to start cycling though the targets. We will be using a On Key Up Trigger with the Right Arrow key option. Now we need to call an Actions list that gathers game objects that should be targeted and cycles them each time this Action is executed.

We'll use the Gather Components By Distance action, which searches around a given radius all objects with a certain component. There are many ways to do this, but our approach would be creating an empty script used to identify which objects can be targeted. To do so, right click on the Project Panel and select Create -> C# Script and give it a name, like TargetableObject. Now drag and drop this script to some objects in your scene that you want to be able to target.

Back the Actions we're going to have the following:

  1. Gather Components by Distance: Fills a List Variables with a collection of targetable objects. In the Component field, type in "TargetableObject" and create a List Variable somewhere in order to fill it.
  2. Game Object Variable: Now that we have the list variable filled, we need to pick an object. This can be easily done using the Game Object variable and storing the selected game object from a list. To cycle between objects, make sure to use the Next option under the Select drop down. This is an iterator that cycles one step at a time through a list variables. You'll also need to create a Local Variable where to store the targeted object. In our case, we created one below the Actions and called it "target"

That's all! Every time you press the Right arrow you'll cycle through different objects that have the TargetableObject component.

You could also use the Trigger "On Variable Change" to detect when the "target" local variable changes and make the camera face whatever object is in "target".

Hope this helps. I'm attaching an image showcasing this.

GOOD, I'M SATISFIED
Satisfaction mark by Crispy 4 months ago
Answer
Answered

Excellent question. You're correct in using the Gather Action. Here's how I would do it:

Disclaimer: While preparing this example we noticed some missing features in version 1.0.1. This will be added in 1.0.2, which should be available tomorrow.

First of all, we need a Trigger to start cycling though the targets. We will be using a On Key Up Trigger with the Right Arrow key option. Now we need to call an Actions list that gathers game objects that should be targeted and cycles them each time this Action is executed.

We'll use the Gather Components By Distance action, which searches around a given radius all objects with a certain component. There are many ways to do this, but our approach would be creating an empty script used to identify which objects can be targeted. To do so, right click on the Project Panel and select Create -> C# Script and give it a name, like TargetableObject. Now drag and drop this script to some objects in your scene that you want to be able to target.

Back the Actions we're going to have the following:

  1. Gather Components by Distance: Fills a List Variables with a collection of targetable objects. In the Component field, type in "TargetableObject" and create a List Variable somewhere in order to fill it.
  2. Game Object Variable: Now that we have the list variable filled, we need to pick an object. This can be easily done using the Game Object variable and storing the selected game object from a list. To cycle between objects, make sure to use the Next option under the Select drop down. This is an iterator that cycles one step at a time through a list variables. You'll also need to create a Local Variable where to store the targeted object. In our case, we created one below the Actions and called it "target"

That's all! Every time you press the Right arrow you'll cycle through different objects that have the TargetableObject component.

You could also use the Trigger "On Variable Change" to detect when the "target" local variable changes and make the camera face whatever object is in "target".

Hope this helps. I'm attaching an image showcasing this.