Complete the code to make the object move forward continuously.
void Update() {
transform.Translate(Vector3.[1] * Time.deltaTime);
}Using Vector3.forward moves the object forward in its local space.
Complete the code to detect obstacles using a raycast.
if (Physics.Raycast(transform.position, transform.forward, [1])) { Debug.Log("Obstacle detected!"); }
The raycast needs a distance as a float value to check for obstacles within that range.
Fix the error in the code to rotate the object away from an obstacle.
if (Physics.Raycast(transform.position, transform.forward, 5f)) { transform.Rotate(0, [1], 0); }
The Rotate method expects a float angle, so 90 (without quotes) is correct.
Fill both blanks to make the object stop moving when an obstacle is detected.
void Update() {
if (Physics.Raycast(transform.position, transform.forward, [1])) {
speed = [2];
} else {
speed = 5f;
}
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}The raycast distance is 5f, and speed is set to 0 to stop movement when an obstacle is detected.
Fill all three blanks to make the object avoid obstacles by turning right and moving forward.
void Update() {
if (Physics.Raycast(transform.position, transform.forward, [1])) {
transform.Rotate(0, [2], 0);
}
transform.Translate(Vector3.forward * [3] * Time.deltaTime);
}The raycast distance is 5f, the object rotates 90 degrees to the right, and moves forward at speed 3f.