Skip to content
Bit Quirky Round Trip
Visual Scripting

Search documentation

All topics and APIs

Close returns focus to Search.

What are you looking for?

Try a common topic or enter a complete API identifier.

Browse the reference instead

Esc closes · Tab reaches results · Enter opens the focused link

v0.89.0
Menu

Turn at the edge

Read an if and else-if pair that reverses a patrol only when it is moving past a bound.

Touching an edge is not enough to turn. EnemyPatrol.FixedUpdate also checks which way the enemy is moving, so it does not flip back and forth while resting on a boundary.

A patrol moves between left and right X bounds. At the left edge it should face right only if it was traveling left. At the right edge it should face left only if it was traveling right. Every other frame keeps the current direction.

  • Two conditions are mutually exclusive and order matters.
  • The second check should run only when the first check is false.
  • A default route should continue without changing the decision value.
The EnemyPatrol FixedUpdate graph with an if and else-if boundary check.
Figure 1. The left-edge test runs first; the right-edge test is its false continuation.

The current Platformer source is:

C#
private void FixedUpdate()
{
var position = _rigidbody.position;
// Reverse when reaching a bound, but only if currently heading further past it, so the
// enemy cannot get stuck flip-flopping on the boundary line.
if (position.x <= _leftBoundX && _direction < 0)
{
_direction = 1;
}
else if (position.x >= _rightBoundX && _direction > 0)
{
_direction = -1;
}
var velocity = _rigidbody.linearVelocity;
velocity.x = _direction * _patrolSpeed;
_rigidbody.linearVelocity = velocity;
}
  1. Open EnemyPatrol.FixedUpdate in Graph View.

  2. Read the position local. It copies the rigidbody position once so both bound checks use the same physics-step value.

  3. Follow the first branch. Its condition combines position.x <= _leftBoundX with _direction < 0. True sets _direction to 1.

  4. Follow the false output into the second branch. This is the else if: position.x >= _rightBoundX and _direction > 0. True sets _direction to -1.

  5. Follow all routes to the velocity sequence. The method copies current velocity, changes only its X component, then writes the struct value back to the rigidbody.

Adding one shared step after the if and else-if, such as orienting the transform from the chosen direction, belongs after both branches join:

Putting that step inside only one branch would skip it on the other turn and on ordinary movement frames.

When it goes wrong

SymptomCheckFix
The enemy jitters at a bound.Read whether each position check also tests direction.Require movement past that same edge before changing direction.
Both edge checks run independently.Follow the false output of the first branch.Connect the right-edge check as else-if on that false route.
Vertical velocity disappears.See whether the code replaces the whole velocity or only velocity.x.Read current velocity, change X, and write the struct back.
Full-size image