Files
fps_project_1/scripts/recoil.gd
2024-11-06 14:02:40 -06:00

47 lines
1.5 KiB
GDScript

extends Node3D
var snap_amount : float = 10
var speed : float = 10
var current_rotation : Vector3
var target_rotation : Vector3
var head_current_rotation : float
var gun_recoil_target_rotation : float = 0
var gun_recoil
@onready var head: Node3D = $".."
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
target_rotation = lerp(target_rotation, Vector3.ZERO, speed * delta)
current_rotation = lerp(current_rotation, target_rotation, snap_amount * delta)
basis = Quaternion.from_euler(current_rotation)
#Handle Gun Recoil
head_current_rotation = lerp(head_current_rotation, gun_recoil_target_rotation, delta * 10)
gun_recoil = lerp(head_current_rotation,gun_recoil_target_rotation, delta * snap_amount)
head.rotation.x = clamp(head.rotation.x + gun_recoil, deg_to_rad(-90), deg_to_rad(85))
gun_recoil_target_rotation -= gun_recoil
func add_recoil(recoil_amount,snap_change,speed_change) -> void:
#set to provided snap/speed changes
snap_amount = snap_change
speed = speed_change
#vertical recoil and random L+R recoil within given range
var recoil_x = recoil_amount.x
var recoil_y = randf_range(-recoil_amount.y,recoil_amount.y)
var recoil_z = randf_range(-recoil_amount.z,recoil_amount.z)
#add to target rotation
target_rotation += Vector3(recoil_x, recoil_y, recoil_z)
func add_gun_recoil(recoil_amount):
gun_recoil_target_rotation += recoil_amount