Skip to content

Instantly share code, notes, and snippets.

@rohanrhu
Created December 30, 2021 18:32
Show Gist options
  • Save rohanrhu/368e24da834b04bed6b491ae95b1e8a3 to your computer and use it in GitHub Desktop.
Save rohanrhu/368e24da834b04bed6b491ae95b1e8a3 to your computer and use it in GitHub Desktop.
# FPS Player
# Copyright (C) 2020 Oğuzhan Eroğlu <[email protected]> (https://oguzhaneroglu.com)
extends KinematicBody
onready var nCamera: Camera = $Camera
export var walk_speed: float = 1
export var sprint_factor: float = 2
export var mouse_sensitivity: float = 0.25
export var gravity = 9.8
onready var nCurrentWeapon = $Camera/AK47
onready var nCurrentWeaponAP = $Camera/AK47/AnimationPlayer
var is_shooting = false
var shoot_time = 0
var shoot_duration = 350
func _ready() -> void:
OS.window_fullscreen = false
func _physics_process(delta: float) -> void:
var input_left = Input.get_action_strength("ui_left")
var input_right = Input.get_action_strength("ui_right")
var input_up = Input.get_action_strength("ui_up")
var input_down = Input.get_action_strength("ui_down")
var input_sprint = Input.is_action_pressed("sprint")
var current_walk_speed = walk_speed
if input_sprint:
current_walk_speed = walk_speed * sprint_factor
$WalkSound.pitch_scale = 1.5
else:
$WalkSound.pitch_scale = 1
var rotation_angle = nCamera.rotation.y
var jump_velocity = 0
var velocity = Vector3(0, jump_velocity, 0)
var mv = (input_up - input_down)
var mh = (input_left - input_right)
var angle = 0
if mv != 0:
if mv > 0:
if mh > 0:
angle = (rotation_angle - PI) + PI/4
elif mh < 0:
angle = (rotation_angle + PI) - PI/4
else:
angle = rotation_angle - PI
elif mv < 0:
if mh > 0:
angle = rotation_angle - PI/4
elif mh < 0:
angle = rotation_angle + PI/4
else:
angle = rotation_angle
else:
if mh > 0:
angle = rotation_angle - PI/2
else:
angle = rotation_angle + PI/2
var dx = sin(angle)
var dy = cos(angle)
if mv != 0 or mh != 0:
if not is_shooting:
nCurrentWeaponAP.play("Walk")
velocity.x = dx
velocity.z = dy
if not $WalkSound.playing:
$WalkSound.play()
elif not is_shooting:
nCurrentWeaponAP.stop()
$WalkSound.stop()
velocity = velocity.normalized() * current_walk_speed
move_and_collide(velocity)
func _process(delta):
if is_shooting and OS.get_ticks_msec() - shoot_time >= shoot_duration:
is_shooting = false
func _input(event) -> void:
if event is InputEventMouseMotion:
nCamera.rotation.x -= deg2rad(event.relative.y * mouse_sensitivity)
nCamera.rotation.x = clamp(nCamera.rotation.x, deg2rad(-90), deg2rad(90))
nCamera.rotate_y(-deg2rad(event.relative.x * mouse_sensitivity))
elif event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if OS.get_ticks_msec() - shoot_time >= 1000:
nCurrentWeaponAP.play("Shoot")
shoot_time = OS.get_ticks_msec()
is_shooting = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment