Okay, I think I have a better idea of what you are wanting to achieve. You want the lion to chase a target, in this case a zebra, actively once it enters the Area node, correct? Do you want the lion to stop chasing the target once it exits the Area? Or do you want to to keep chasing until it reaches the target (or some other condition)?
For tracking the zebra once it enters the Area node, there are a couple ways you can handle this depending on how you want tracking to work.
I believe the code I posted previously will work if you want the lion to stop chasing the target once it exists the Area node with a little tweaking. The following code should actively track the position of the target once it enters the Area, but to stop tracking when it the target is no longer in the Area (untested):
extends Spatial
# Set the position to Null to signify we do not have a target
var _target_position = null
# Save the node we want to use as a target in case there
# are multiple targets within the Area (optional)
var _target_node = null
# I'm assuming the Area node is separate from the lion script, meaning
# we will need a reference to it before we can use it in this script
var area_node = null
func _ready():
# Get the Area node
area_node = get_node("Area")
func _physics_process(_delta):
# Create a boolean so we can track if the target it still in the area or not
var target_in_area = false
# Get all of the bodies in the Area node
var bodies = area_node.get_overlapping_bodies()
for body in bodies:
# Check if the body is the target. For this example I will assume the name
# of the target node(s) are "Zebra"
if body.name == "Zebra":
# Do we have a target already?
if _target_node != null:
# Is this body the target?
if body == target:
# Set target_in_area to true and update the target position
target_in_area = true
_target_position = body.global_transform.origin
# If the body is not the target, then do nothing/ignore it
# If we do not have a target...
else:
# Set this body as the target, set target_in_area to true, and
# update the target position
_target_node = body
target_in_area = true
_target_position = body.global_transform.origin
# If we have a target and did not find it in the Area node...
if (_target_node != null and target_in_area == false):
# Reset _target_node and _target_position
_target_node = null
_target_position = null
# Check if we have a target position...
if (_target_position != null):
# Do whatever you need to do to chase the target here!
pass
Alternatively, you can also use signals in the Area, however this will not hunt the next target in the Area, but instead a target will have to enter the area to trigger the lion to chase it, but the code might be a little better for performance. The code could look something like the following (untested):
extends Spatial
var _target_position = null
var _target_node = null
var area_node = null
func _ready():
# Get the Area node
area_node = get_node("Area")
# Connect the signals
area_node.connect("body_entered", self, "_on_body_entered_area")
area_node.connect("body_exited", self, "_on_body_exited_area")
func _on_body_entered_area(other):
# Check if the body is the target. For this example I will assume the name
# of the target node(s) are "Zebra"
if other.name == "Zebra":
# Make sure we do not already have a target...
if _target_node == null:
# Set the body as the target
_target_node = other
func _on_body_exited_area(other):
# Check to see if we have a target...
if _target_node != null:
# See if the body that left is the target...
if other == _target_node:
# Clear the position and stop tracking the target
_target_position = null
_target_node = null
func _physics_process(_delta):
# Check if we have a target...
if _target_node != null:
# Get the position of the target
_target_position = _target_node.global_transform.origin
# Do whatever you need to do to chase the target here!
pass
This post is getting a tad long, so I'll post the other way in the next reply.