Hi, im not great with coding yet, I just started recently. So I've tried 2 different tutorials, and they both work in making the screen shake but I am just confused as how I am supposed to call these every time my player jumps and kills an enemy in my player script. This is what I have currently. The problem is that once that shaking starts, it doesn't stop. I'm thinking this is because I'm not calling the screen shake properly in my player script, if you're kind enough to answer a probably stupid question like this, that would be amazing!
My Player Script
extends Actors
export var stomp_impulse = 1500.0
onready var anim: AnimationPlayer = $AnimationPlayer
onready var anim2: AnimationPlayer = $AnimationPlayer2
var can_glide = false
var can_attack = true
var glideing = false
var glide_direction = Vector2(1,0)
var is_jumping: bool
var snap = Vector2.DOWN*32 if is_on_floor() else Vector2.ZERO
func _on_FallBoundary_body_entered(body: Node) -> void:
#level 2
die()
func _on_Area2D_body_entered(body: Node) -> void:
#level 1
die()
func _on_EnemyDetector_body_entered(body: Node) -> void:
die()
func _input(ev):
#RestartButton
if ev is InputEventKey and ev.scancode == KEY_R:
get_tree().reload_current_scene()
if Input.is_action_just_pressed("down"): anim.play("Attack")
func _on_EnemyDetector_area_entered(area: Area2D) -> void:
$Camera2D/Node.start_shake()
_velocity = calculate_stomp_velocity(_velocity, stomp_impulse)
anim2.play("Bonk")
func _on_AttackArea_area_entered(area: Area2D) -> void:
_velocity = calculate_stomp_velocity(_velocity, stomp_impulse)
func _physics_process(delta: float) -> void:
var is_jump_interrupted: = Input.is_action_just_released("Jump") and _velocity.y < -0.5
var direction: = get_direction()
_velocity = calculate_move_velocity(_velocity,direction,speed, is_jump_interrupted)
_velocity = move_and_slide_with_snap(_velocity, snap, FLOOR_NORMAL)
glide()
glideing()
func glide():
if is_on_floor():
can_glide = true # recharges when player touches the floor
if Input.is_action_pressed("Move_Right"):
glide_direction = Vector2(1,0)
if Input.is_action_pressed("Move_Left"):
glide_direction = Vector2(-1,0)
func glideing():
if glideing: gravity = 0
if not glideing: gravity = 4000.0
if Input.is_action_just_pressed("glide") and can_glide:
_velocity = Vector2(2000, 0)
can_glide = false
glideing = true # turn off gravity while glideing
yield(get_tree().create_timer(0.2), "timeout")
glideing = false
func get_direction() -> Vector2:
return Vector2(
Input.get_action_strength("Move_Right") - Input.get_action_strength("Move_Left"),
-1.0 if Input.is_action_just_pressed("Jump") and is_on_floor() else 1.0
)
func calculate_move_velocity(
linear_velocity: Vector2,
direction: Vector2,
speed: Vector2,
is_jump_interrupted: bool
) -> Vector2:
var out: = linear_velocity
out.x = speed.x * direction.x
out.y += gravity * get_physics_process_delta_time()
if direction.y == -1.0:
out.y = speed.y * direction.y
if is_jump_interrupted:
out.y = 0.0
return out
func calculate_stomp_velocity(linear_velocity: Vector2, impulse: float) -> Vector2:
var out: = linear_velocity
out.y = -impulse
return out
func die() -> void:
PlayerData.deaths += 1
get_tree().reload_current_scene()
queue_free()
`
Where I'm calling the screen shake
func _on_EnemyDetector_area_entered(area: Area2D) -> void:
$Camera2D/Node.start_shake()
_velocity = calculate_stomp_velocity(_velocity, stomp_impulse)
My Screen Shake Script
extends node
const TRANS := Tween.TRANS_SINE
const EASE := Tween.EASE_IN_OUT
var amplitude := 0
var priority := 0
onready var camera := get_parent() as Camera2D
onready var Duration := $Duration as Timer
onready var Frequency := $Frequency as Timer
onready var ShakeTween := $ShakeTween as Tween
func start_shake(duration: float = 0.2, frequency: float = 15, amplitude: float = 16, priority: float = 0):
if priority >= self.priority:
self.priority = priority
self.amplitude = amplitude
Duration.wait_time = duration
Frequency.wait_time = 1 / float(frequency)
Duration.start()
Frequency.start()
_new_shake()
func _new_shake() -> void:
randomize()
var rand := Vector2()
rand.x = rand_range(-amplitude, amplitude)
rand.y = rand_range(-amplitude, amplitude)
ShakeTween.interpolate_property(camera, "offset", camera.offset, rand, Frequency.wait_time, TRANS, EASE)
ShakeTween.start()
func _reset() -> void:
ShakeTween.interpolate_property(camera, "offset", camera.offset, Vector2(), Frequency.wait_time, TRANS, EASE)
ShakeTween.start()
priority = 0
func _on_Frequency_timeout():
_new_shake()
func _on_Duration_timeout():
_reset()
Frequency.stop()
`