0
0
Unityframework~10 mins

Obstacle avoidance in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Obstacle avoidance
Start
Sense obstacles
Is obstacle close?
NoMove forward
Yes
Calculate avoidance direction
Change movement direction
Move
Repeat
The agent senses obstacles, decides if it needs to avoid, calculates a new direction, and moves accordingly.
Execution Sample
Unity
void Update() {
  if (Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, 5f)) {
    Vector3 avoidDir = Vector3.Reflect(transform.forward, hit.normal);
    transform.rotation = Quaternion.LookRotation(avoidDir);
  }
  transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
This code checks for obstacles ahead and changes direction to avoid them while moving forward.
Execution Table
StepRaycast HitObstacle DistanceCondition (hit < 5)ActionMovement Direction
1No-FalseMove forwardForward
2Yes3TrueCalculate avoidDir, rotateAvoidance direction
3No-FalseMove forwardForward
4Yes4TrueCalculate avoidDir, rotateAvoidance direction
5No-FalseMove forwardForward
Exit--No more updatesStop or continue loop-
💡 Loop continues each frame; stops when game ends or object destroyed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
transform.position(0,0,0)(0,0,0.1)(0,0,0.1)(0,0,0.2)(0,0,0.2)Updated each frame
transform.forward(0,0,1)(0,0,1)Reflected vectorReflected vectorReflected vectorChanges on avoidance
hit.distance--3-4-
avoidDir--(calculated)-(calculated)-
Key Moments - 3 Insights
Why does the object sometimes not avoid obstacles even when close?
Because the raycast might not hit if the obstacle is outside the ray's range or angle, as shown in steps 1 and 3 where no hit occurs.
Why do we use Vector3.Reflect for avoidance direction?
Vector3.Reflect bounces the forward direction off the obstacle's surface normal, giving a smooth avoidance direction (see step 2 and 4 actions).
Why is movement done after rotation?
Rotation changes the facing direction first, so movement forward goes in the new direction, ensuring the object avoids the obstacle effectively.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the movement direction at step 2?
AForward
BBackward
CAvoidance direction
DNo movement
💡 Hint
Check the 'Movement Direction' column at step 2 in the execution table.
At which step does the raycast detect an obstacle at distance 4?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look at the 'Obstacle Distance' column in the execution table.
If the raycast range is increased from 5 to 10, how would the execution table change?
AMore steps with 'Yes' raycast hit and avoidance actions
BFewer avoidance actions
CNo change in raycast hits
DMovement direction becomes backward
💡 Hint
Increasing raycast range means detecting obstacles earlier, so more 'Yes' hits in the table.
Concept Snapshot
Obstacle avoidance in Unity:
- Use Physics.Raycast forward to detect obstacles
- If hit within range, calculate avoidance direction with Vector3.Reflect
- Rotate object toward avoidance direction
- Move forward each frame
- Repeat to keep avoiding obstacles dynamically
Full Transcript
In obstacle avoidance, the object uses a raycast to sense obstacles ahead. If an obstacle is detected within a certain distance, it calculates a new direction by reflecting its forward vector off the obstacle's surface. Then it rotates to face this new direction and moves forward. If no obstacle is detected, it continues moving forward. This process repeats every frame to keep the object moving safely around obstacles.