Alright i think i have a (temporary) solution, so using an onready var just doesn't do anything, not even an error/warning. using a var errors because it cant find it. but just pointing to the node through the statement and not the variable makes it work like intended. But if you want to review the code still here it is.
extends KinematicBody
var speed = 10
var h_acceleration = 6
var air_acceleration = 1
var normal_acceleration = 6
var gravity = 50
var jump = 20
var full_contact = false
var aiming = false
var paused = false
var damage = 100
var mouse_sensitivity = 0
var mainsensitivity = 0.03
const SWAY = 30
const gunClipSize = 10
var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()
onready var head = $Head
onready var camera = $Head/Camera
onready var crosshair = $Head/Camera/Crosshair
onready var ground_check = $GroundCheck
onready var aimcast = $Head/Camera/AimCast
onready var muzzle = $Head/gun20/MoveGun/Muzzle
onready var muzzleflash = $Head/gun20/MoveGun/Muzzle/Flash
onready var fireanim = $Head/gun20/Fire
onready var aimanim = $Head/gun20/Aim
onready var walkanim = $Head/gun20/Walk
onready var firesound = $Head/gun20/Shoot
onready var gun = $Head/gunLoc
onready var gunloc = $Head/gun20
onready var gui = get_parent().get_node("UI")
onready var gunAmmoLeft = $Head/gun20.ClipAmmo
onready var gunAmmoLefttostr = String($Head/gun20.ClipAmmo)
onready var extragunAmmo = $Head/gun20.ExtraAmmo
onready var extragunAmmotostr = String($Head/gun20.ExtraAmmo)
onready var currentWeapon = $Head/gun20.CurrentWeapon
onready var gunAmmoLeftText = get_parent().get_node("UI/HUD/Ammo/AmmoLeft").text
onready var gunExtraAmmoText = get_parent().get_node("UI/HUD/Ammo/ExtraAmmo").text
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
gunloc.set_as_toplevel(true)
camera.fov = 91
gunAmmoLeftText = gunAmmoLeft
yield(get_tree().create_timer(0.5), "timeout")
mouse_sensitivity = mainsensitivity
func _input(event):
if event is InputEventMouseMotion:
rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))
func _process(delta):
gunloc.global_transform.origin = lerp(gunloc.global_transform.origin, gun.global_transform.origin, SWAY * delta)
gunloc.rotation.y = lerp_angle(gunloc.rotation.y, rotation.y, SWAY * delta)
gunloc.rotation.x = lerp_angle(gunloc.rotation.x, head.rotation.x, SWAY * delta)
func flash(obj, length):
obj.visible = true
yield(get_tree().create_timer(length), "timeout")
obj.visible = false
func _physics_process(delta):
direction = Vector3()
if gui.get_node("Pause/Background/Resume").pressed == true:
gui.get_node("Pause").visible = false
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
mouse_sensitivity = mainsensitivity
yield(get_tree().create_timer(0.05), "timeout")
paused = false
if Input.is_action_just_pressed("pause"):
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
mouse_sensitivity = 0
gui.get_node("Pause").visible = true
paused = true
else:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
mouse_sensitivity = mainsensitivity
gui.get_node("Pause").visible = false
yield(get_tree().create_timer(0.01), "timeout")
paused = false
if paused == false:
if Input.is_action_just_pressed("aim"):
aimanim.play("Aim")
crosshair.visible = false
aiming = true
elif Input.is_action_just_released("aim"):
aimanim.play_backwards("Aim")
crosshair.visible = true
aiming = false
speed = Input.get_action_strength("move_forward") * 10
if Input.is_action_just_pressed("fire"):
if currentWeapon == "gun":
gunAmmoLeft = gunAmmoLeft - 1
#gunAmmoLefttostr = float(gunAmmoLeft)
gunAmmoLefttostr = str(gunAmmoLeft)
gunAmmoLeftText = gunAmmoLefttostr
print(gunAmmoLeft)
fireanim.stop()
fireanim.play("Fire")
firesound.play()
flash(muzzleflash, 0.2)
if aimcast.is_colliding():
var bullet = get_world().direct_space_state
var collision = bullet.intersect_ray(muzzle.transform.origin, aimcast.get_collision_point())
if collision:
var target = collision.collider
if target.is_in_group("Enemy"):
print("hit enemy")
target.health -= damage
if ground_check.is_colliding():
full_contact = true
else:
full_contact = false
if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
gravity_vec = Vector3.UP * jump
if Input.is_action_pressed("move_forward"):
speed = Input.get_action_strength("move_forward") * 10
direction -= transform.basis.y
elif Input.is_action_pressed("move_backward"):
speed = Input.get_action_strength("move_backward") * 10
direction += transform.basis.y
if Input.is_action_pressed("move_left"):
speed = Input.get_action_strength("move_left") * 10
direction -= transform.basis.x
elif Input.is_action_pressed("move_right"):
speed = Input.get_action_strength("move_right") * 10
direction += transform.basis.x
direction = direction.normalized()
h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
movement.z = h_velocity.z + gravity_vec.z
movement.x = h_velocity.x + gravity_vec.x
movement.y = gravity_vec.y
move_and_slide(movement, Vector3.UP)
if not is_on_floor():
gravity_vec += Vector3.DOWN * gravity * delta
h_acceleration = air_acceleration
elif is_on_floor() and full_contact:
gravity_vec = -get_floor_normal() * gravity
h_acceleration = normal_acceleration
else:
gravity_vec = -get_floor_normal()
h_acceleration = normal_acceleration
