0 votes

Hello, sorry if the wording is a bit jumbled. Here's the basic idea:

I'm making a 3D FPS title where one of the weapons is a shotgun which moves the player character a few units back after shooting. My question is this: how do i maintain the velocity from the shotgun blast? Here's a video to better demonstrate what I'm talking about:
https://youtu.be/K5BmOYF6ZWc (yes, the video is also a mess, but at least it's comprehensible)

Here's the relevant piece of code:

var input_move: = Vector3()
var gravity_local: = Vector3()
var snap_vector: = Vector3()
var velocity = Vector3.ZERO
export(int) var jump_speed: = 12

var shotgun_speed_active = false
var shotgun_shoot_active = false
export(float) var shotgun_movement_timer: = 0.2
export(float) var shotgun_shoot_timer: = 1.0
export(int) var shotgun_movement_speed: = 3
export(float) var shotgun_damage: = 40.0
export(int) var shotgun_spread: = 20
var shotgun_pos = Vector3()

func _physics_process(delta):   
Global.player_pos = $TargetPos.global_transform.origin
input_move = _get_directions()

if not is_on_floor():
    gravity_local += Vector3.DOWN * GRAVITY_ACCELERATION * delta
    snap_vector = Vector3.ZERO
    gravity_local = Vector3.ZERO

snap_vector = Vector3.DOWN

if is_on_floor():
    snap_vector = -get_floor_normal()

if Input.is_action_just_pressed("jump") and is_on_floor():
    snap_vector = Vector3.ZERO
    gravity_local = Vector3.UP * jump_speed 

if Input.is_action_just_pressed("fire") and can_shoot:  
    match current_weapon:
            if !shotgun_shoot_active:
if shotgun_speed_active:
    move_and_slide_with_snap(((shotgun_pos * MAX_SPEED) * 4.2) + gravity_local , snap_vector, Vector3.UP)
    move_and_slide_with_snap(((input_move * MAX_SPEED) * dash_speed) + gravity_local , snap_vector, Vector3.UP)

func shoot_shotgun():
shotgun_shoot_active = true
for r in $LookPivot/shotgunSpatial.get_children():
    r.cast_to.x = rand_range(shotgun_spread, -shotgun_spread)
    r.cast_to.y = rand_range(shotgun_spread, -shotgun_spread)
    if r.is_colliding() and r.get_collider().is_in_group("enemy"):

shotgun_pos = $LookPivot.global_transform.basis.z.normalized()  
snap_vector = Vector3.ZERO
gravity_local = Vector3.UP * shotgun_movement_speed

shotgun_speed_active = true
yield(get_tree().create_timer(shotgun_movement_timer), "timeout")
shotgun_speed_active = false

yield(get_tree().create_timer(shotgun_shoot_timer), "timeout")
shotgun_shoot_active = false    

Yes, it's not a pretty piece of code, but whatever. I basically want this to function akin to rocket jumping from Quake and/or TF2. Any piece of advice would be greatly appreciated!

Godot version Godot 3.5
in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.