How do I aim and fire my bot's laser?
Fire on what the scanner sees
from bot import Chassis, Scanner, Laser, wait
chassis = Chassis(0)
scanner = Scanner(1)
laser = Laser(2)
while True:
if scanner.ping(0):
b = scanner.bearing(0)
scanner.sense = b # keep the arc on it
if laser.ready:
laser.fire(b) # the turret turns to b and fires
else:
scanner.sense = chassis.heading
wait()
A bot that fires whenever it's ready fires every 10 ticks.
How a shot works
- Aim: the bearing is a field bearing in degrees (0 east, positive to the left), the same as the scanner's. You don't need to turn the chassis to shoot.
- Range: the beam stops at the first bot or obstacle within 30 m, the same range as the scanner.
- Damage: 10 hull points to everything within 0.78 m of where the beam stops.
- Timing: like every command, the shot goes out at your next
wait(). A shot is a one-off: it doesn't repeat on later ticks. - Cooldown: a shot sent while the laser is cooling down is dropped, not saved for later. So check
laser.readyfirst.
Don't hit your own side
Friendly fire is on. A teammate near where the beam stops is hit too, and so is your own bot if it fires at something it's touching. The beam also stops at the first thing in its way, so a teammate between you and the enemy takes the shot.
- Carry a radio and check
radio.teammate(): hold fire when your teammate is in line with the target. The house bot seeker does this (pick it from Start from… in a new Brain code tab). - Back off before firing at something you're pressed against.
- A contact at almost exactly 0, 90, −90 or −180 degrees is usually a wall or block face, since they line up with the field. The seeker doesn't fire at those.
More than one laser
Two lasers on different channels each have their own cooldown:
if laser.ready:
laser.fire(b)
elif laser2.ready:
laser2.fire(b)
Here laser2 = Laser(3) connects the second one. Other weapons, such as rockets, mortars and missiles, aim and fire in their own ways; the Reference in Brain code covers each.