User Tools

Site Tools


dragengine:modules:dragonscript:behavior_lookat

ECBehaviorLookAt

Behavior adding support to actors to know what element they are looking at.

Behavior uses one collider collision test located attached ECBehaviorColliderAI facing forward. If no ECBehaviorColliderAI is present ECBehaviorCollider is used instead.

If ECBehaviorComponent is present and a test bone is set the collision test will be attached to the component bone. This is simple and good solution for FPS type games.

If no component and bone is used and ECBehaviorLocomotion is present then the test parameters will be updated to match the locomotion looking direction.

Instance Counts

This behavior can be used only once on an element.

Element Class Properties

Element class properties have the prefix lookAt. .

range

Set range in meters. Elements will only be detected this far.

  • Full name: lookAt.range
  • Type: float
  • Default Value: 2
  • Restriction: At least 0
  • Example (*.deeclass)
    <float name='lookAt.range'>3</float>

origin

Set origin from where the test ray is cast.

  • Full name: lookAt.origin
  • Type: 3-component float vector
  • Default Value: (0,1.6,0)
  • Example (*.deeclass)
    <vector name='lookAt.origin' x='0' y='1.6' z='0.2'/>

bone

Set bone from where the test ray is cast.

  • Full name: lookAt.bone
  • Type: string
  • Default Value: empty string
  • Example (*.deeclass)
    <string name='lookAt.bone'>head</string>

Events

This behavior has these events:

enabledChanged

Enabled changed.

lookAtChanged

Looking at element changed.

Behavior Tree Actions

This behavior adds these behavior tree actions if behavior tree is present.

lookAt.check

Check one or more look-at parameters. Action succeeds if all parameter value matches their respective look-at parameter otherwise action fails. This action is typically used as first action in a sequence to run the sequence only if a look-at parameter matches (or not).

ParameterValueDescription
enabledtrue, falseLook-at is enabled
lookAttrue, falseIs looking at element
lookAt.distance.lessfloatDistance to look-at element is less than value meters
lookAt.distance.greaterfloatDistance to look-at element is greater than value meters
lookAt.playertrue, falseIs looking at player
wait If present action returns BTResult.running instead of BTResult.failed to wait until the checks are all fulfilled

This is an example of using this action:

<sequence>
  <action name='lookAt.check'>
    <parameter name='lookAt.player'>true</parameter>
  </action>
  <!-- actions here run only if looking at player -->
</sequence>

Behavior Tree Conditions

This behavior adds these behavior tree conditions if behavior tree is present.

lookAt.check

Check one or more look-at parameters. Conditions returns true if all parameter value match their respective look-at parameter. This condition is typically used to run an action or sequence of actions as long as look-at conditions are true.

ParameterValueDescription
enabledtrue, falseLook-at is enabled
lookAt.lookAttrue, falseIs looking at element
lookAt.lookAt.distance.lessfloatDistance to look-at element is less than value meters
lookAt.lookAt.distance.greaterfloatDistance to look-at element is greater than value meters
lookAt.lookAt.playertrue, falseIs looking at player

This is an example of using this condition:

<action name='myAction' id='doing something'>
  <parameter name='lookAt.enabled'>true</parameter>
  <condition>lookAt.check</condition>
</action>

State Machine Actions

State Machine Conditions

State Machine Events

This behavior sends these state machine events.

lookAt.enabled

Look-at has been enabled.

lookAt.disabled

Look-at has been disabled.

lookAt.lookAt

Looking at an element. Send if looking at an element when previously not looking at an element or looking at a different element.

lookAt.noLookAt

Not looking at an element. Send if not looking at an element when previously has been looking at an element.

Required Behaviors

This behavior requires no other behaviors.

Optional Behaviors

Persistency

This behavior does not required element class to be persistable (setPersistable).

API Documentation

ECBehaviorLookAt.

Since DragonScript Module Version 1.0

Use Cases

  • Detect what element the player or an NPC is looking at.

Element Class Example

This example defines an element which look-at support.

class MyElement extends BehaviorElementClass
  public var ECBehaviorComponent component
  public var ECBehaviorCollider collider
  public var ECBehaviorColliderAI colliderAI
  public var ECBehaviorLocomotion locomotion
  public var ECBehaviorLookAt lookAt
  func new()
    component = ECBehaviorComponent.new(this, null)
    collider = ECBehaviorCollider.new(this, component)
    colliderAI = ECBehaviorColliderAI.new(this, collider)
    locomotion = ECBehaviorLocomotion.new(this, colliderAI)
    lookAt = ECBehaviorLookAt.new(this, colliderAI, component, locomotion)
  end
end

Behavior Factory

Using element class supporting adding behaviors the behavior can be added like this:

<?xml version='1.0' encoding='UTF-8'?>
<elementClass name='MyClass' class='GenericBehaviorElement'>
  <behavior type='ECBehaviorComponent'/>
  <behavior type='ECBehaviorCollider'/>
  <behavior type='ECBehaviorColliderAI'/>
  <behavior type='ECBehaviorLocomotion'/>
 
  <behavior type='ECBehaviorLookAt'>
    <!-- optional: set collision filter. default value '3:0 1 2 3' which means
                   category BaseGameApp.CollisionFilterBit.actorAI
                   filter BaseGameApp.CollisionFilterBit.geometry,
                          BaseGameApp.CollisionFilterBit.dynamic,
                          BaseGameApp.CollisionFilterBit.actorAI.
                   format is '', 'category' or 'category:filter' where category and filter
                   are a list of bits to set. -->
    <string name='collisionFilter'>3:0 1 2 3</string>
 
    <!-- optional: use component with id instead of empty string -->
    <string name='component'>second</string>
 
    <!-- optional: use behavior tree with id instead of empty string -->
    <string name='behaviorTree'>second</string>
 
    <!-- optional: use state machine with id instead of empty string -->
    <string name='stateMachine'>second</string>
 
    <!-- set element properties. omit property prefix if used inside behavior tag -->
    <string name='.name'>Color 1</string>
  </behavior>
 
  <!-- for adding multiple behaviors use unique identifiers -->
  <behavior type='ECBehaviorLookAt' id='second'/>
</elementClass>

Live Examples

You could leave a comment if you were logged in.
dragengine/modules/dragonscript/behavior_lookat.txt · Last modified: 2025/05/04 13:44 by dragonlord