How would i go about scripting a minigun?

Godot Version

godot4.4

Question

how to set a code for the barrels for when i press the aim button it would spin the barrels to the max speed smoothly do i use a curve ? and when i hit the maximum rotation speed i would be able to shot and after i release the aim and shooting button the barrels would stop rotating and go to the original rotation position, any help is appreciated :+1:

What sort of animations/art do you have?

In the abstract, the code could look something like this:

# Semi-pseudocode, untested.

const SPINUP_TIME:   float = 1.2  # 1.2 seconds
const SPINDOWN_TIME: float = 0.8  # 0.8 seconds
const SHOOT_TIME:    float = 0.05 # 20 shots/sec

enum GunState {idle, spinup, shoot, spindown}

var state:      GunState = GunState.idle
var time:       float    = 0.0
var spin_speed: float    = 0.0

func start_shooting() -> void:
    match(state):
        GunState.idle:     time  = 0.0
        GunState.spinup:   pass
        GunState.shoot:    pass
        GunState.spindown: time *= (SPINDOWN_TIME / SPINUP_TIME) # rescale
    state = GunState.spinup

func stop_shooting() -> void:
    match(state):
        GunState.idle:     pass
        GunState.spinup:   time *= (SPINUP_TIME / SPINDOWN_TIME) # rescale
        GunState.shoot:    time  = 0.0
        GunState.spindown: pass
    state = GunState.spindown

func _process(delta: float) -> void:
    match(state):
        GunState.idle:
            pass
        GunState.spinup:
            time = minf(time = delta, SPINUP_TIME)
            spin_speed = time / SPINUP_TIME # Could pass this through `ease()`
            if time >= SPINUP_TIME:
                time = 0.0
                state = GunState.shoot
        GunState.shoot:
            time += delta
            while time > SHOOT_TIME :
                emit_shot()
                time -= SHOOT_TIME
        GunState.spindown:
            time = minf(time + delta, SPINDOWN_TIME)
            spin_speed = time / SPINDOWN_TIME # Could pass this through `ease()`
            if time >= SPINDOWN_TIME:
                time = 0.0
                state = GunState.idle
1 Like

thank you for your help.

1 Like