Created
January 24, 2024 10:07
-
-
Save it-ankka/d097951489ea33cd3fdee5c53cc26636 to your computer and use it in GitHub Desktop.
Simple Godot first-person weapon sway, bob and tilt component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name PlayerJuiceComponent extends Node | |
@onready var player: Player = get_parent() | |
@export_group("General") | |
@export var LERP_SPEED: float = 10 | |
@export_group("Bob") | |
@export var BOB_AMPLITUDE: Vector2 = Vector2.ONE * 0.01; | |
@export var BOB_FREQ: Vector2 = Vector2.ONE * 0.01; | |
@export_group("Tilt") | |
@export var HEAD_ROTATION_AMOUNT: float = 0.04; | |
@export var JUMP_ARMS_TILT: float = 0.1; | |
@export_group("Sway") | |
@export var ARMS_SWAY_AMOUNT: float = 0.01; | |
@export var ARMS_ROTATION_AMOUNT: float = 0.04; | |
func _physics_process(delta: float) -> void: | |
var is_moving = player.velocity.length() > 0 and player.is_on_floor() | |
apply_tilt(player.head, 2, player.input_dir.x, HEAD_ROTATION_AMOUNT, LERP_SPEED, delta) | |
apply_tilt(player.arms, 2, player.input_dir.x, ARMS_ROTATION_AMOUNT, LERP_SPEED, delta) | |
if not player.is_on_floor(): | |
apply_tilt(player.arms, 0, player.velocity.y, JUMP_ARMS_TILT, LERP_SPEED, delta) | |
apply_sway(player.arms, player.mouse_dir, ARMS_SWAY_AMOUNT, false, LERP_SPEED, delta) | |
apply_bob(player.arms, player.arms_default_pos, is_moving, BOB_AMPLITUDE, BOB_FREQ, LERP_SPEED, delta) | |
func apply_tilt(target: Node3D, axis: int, scale: float, amount: float, lerp_speed: float, delta: float): | |
if target: | |
target.rotation[axis] = lerp(target.rotation[axis], -scale * amount, lerp_speed * delta) | |
func apply_sway(target: Node3D, target_dir: Vector2, amount: float, inverted: bool, lerp_speed: float, delta: float): | |
var dir: Vector2 = lerp(target_dir,Vector2.ZERO, lerp_speed * delta) | |
target.rotation.x = lerp(target.rotation.x, dir.y * amount * (-1 if inverted else 1), lerp_speed * delta) | |
target.rotation.y = lerp(target.rotation.y, dir.x * amount * (-1 if inverted else 1), lerp_speed * delta) | |
func apply_bob(target: Node3D, def_target_pos: Vector3, is_moving: bool, bob_amplitude: Vector2, bob_freq: Vector2, lerp_speed: float, delta: float): | |
if target: | |
if is_moving: | |
target.position.y = lerp(target.position.y, def_target_pos.y + sin(Time.get_ticks_msec() * bob_freq.y) * bob_amplitude.y, lerp_speed * delta) | |
target.position.x = lerp(target.position.x, def_target_pos.x + sin(Time.get_ticks_msec() * bob_freq.x) * bob_amplitude.x, lerp_speed * delta) | |
else: | |
target.position.y = lerp(target.position.y, def_target_pos.y, lerp_speed * delta) | |
target.position.x = lerp(target.position.x, def_target_pos.x, lerp_speed * delta) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment