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
else:
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:
WEAPON_SELECT.PISTOL:
$LookPivot/GunMesh/AnimationPlayer.play("RESET")
shoot_pistol()
WEAPON_SELECT.SHOTGUN:
if !shotgun_shoot_active:
shoot_shotgun()
if shotgun_speed_active:
move_and_slide_with_snap(((shotgun_pos * MAX_SPEED) * 4.2) + gravity_local , snap_vector, Vector3.UP)
else:
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"):
r.get_collider().hit(shotgun_damage)
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!