`I have a plant that shoots bullets when a player is within range. I added the shoot function on the onUpdate of the plant and the plant is shooting bullets in a stream…i want it sparsly maybe like one or two in a few seconds if that makes sense.
@override
void update(double dt) {
if (!gotStomped) {
_updateState();
_shoot();
}
super.update(dt);
}
void _shoot() {
if (playerInRange()) {
moveDirection = lerpDouble(moveDirection, tragetDirection, 0.1) ?? 1;
current = State.attack;
double playerOffset = (player.scale.x > 0) ? 0 : -player.width;
double chickenOffset = scale.x > 0 ? 0 : -width;
tragetDirection =
(player.x + playerOffset < position.x + chickenOffset) ? -1 : 1;
Bullet bullet = Bullet(
position: Vector2(position.x, position.y),
size: Vector2.all(16),
tragetDirection: tragetDirection);
parent?.add(bullet);
} else {
current = State.idle; // Back to idle state
}
}
}
New contributor
rikeelesh ramjattun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.