From d86efbb357c2d42dd6bead3c0ec646761cf11c69 Mon Sep 17 00:00:00 2001 From: derek Date: Wed, 6 Nov 2024 14:02:40 -0600 Subject: [PATCH] improved recoil, fixing bounce --- assets/mac_10.tscn | 1 + scripts/gun.gd | 3 ++- scripts/recoil.gd | 16 ++++++++++++++++ scripts/revolver_1.gd | 3 ++- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/assets/mac_10.tscn b/assets/mac_10.tscn index e96cffa..9f16ea2 100644 --- a/assets/mac_10.tscn +++ b/assets/mac_10.tscn @@ -404,6 +404,7 @@ _data = { [node name="mac10" node_paths=PackedStringArray("flare_light", "anim_player", "barrel_raycast", "casing_ejector", "mag_ejector", "audio_fire", "audio_empty", "audio_reload") instance=ExtResource("1_nb4p5")] script = ExtResource("2_tskiy") gun_name = "Mac 10" +recoil_amount = Vector3(0.05, 0.05, 0.05) max_ammo = 20 bullet_damage = 2 bullet_speed = 1500 diff --git a/scripts/gun.gd b/scripts/gun.gd index 0b2f76b..652c5a4 100644 --- a/scripts/gun.gd +++ b/scripts/gun.gd @@ -94,7 +94,8 @@ func shoot(delta): hitscan_fire() spawn_casing() - player.recoil.add_recoil(recoil_amount,10,10) + player.recoil.add_recoil(Vector3(0,recoil_amount.y,recoil_amount.z),10,10) + player.recoil.add_gun_recoil(recoil_amount.x) if fire_mode != 0: cycle_count -= 1 diff --git a/scripts/recoil.gd b/scripts/recoil.gd index 6218813..3e6c13d 100644 --- a/scripts/recoil.gd +++ b/scripts/recoil.gd @@ -5,6 +5,13 @@ 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. @@ -16,6 +23,12 @@ func _process(delta: float) -> void: 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 @@ -28,3 +41,6 @@ func add_recoil(recoil_amount,snap_change,speed_change) -> void: #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 diff --git a/scripts/revolver_1.gd b/scripts/revolver_1.gd index 2a67c3a..945bce0 100644 --- a/scripts/revolver_1.gd +++ b/scripts/revolver_1.gd @@ -157,7 +157,8 @@ func fire(delta): instance_bullet.player_velocity = (player.velocity * player.transform.basis) instance_bullet.instance_bullethole = bullethole.instantiate() get_tree().get_root().add_child(instance_bullet) - player.recoil.add_recoil(recoil_amount,10,recoil_speed_change) + player.recoil.add_recoil(Vector3(0,recoil_amount.y,recoil_amount.z),10,recoil_speed_change) + player.recoil.add_gun_recoil(recoil_amount.x) chamber.rotate_object_local(Vector3(0,-1,0),deg_to_rad(60)) func reload(delta):