I am making a game where if you do anything in the game the hunger goes up, but if you stand in place then static electricity will build up and then you get struck by lightning and die. But when i stand still the thing just builds up immediatly with no pauses in between, how do i fix it?
Here’s the code of my player:
extends CharacterBody3D
@onready var CameraController = $Cameracontroller
const Gravity = 3
var Speed = 2
var idle = true
var pickingup = false
var sticks = 0
var static_electricity = 0
func _process(delta: float) -> void:
if not is_on_floor():
velocity.y += -Gravity * delta #gravity
if Input.is_action_just_pressed("CameraLeft"): #turn camera left
CameraController.rotate_y(0.5)
if Input.is_action_just_pressed("CameraRight"): #turn camera right (i think)
CameraController.rotate_y(-0.5)
var input_dir = Input.get_vector("Move_Left", "Move_Right", "Move_Forward", "Move_Backwards")
var direction = (CameraController.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * Speed
velocity.z = direction.z * Speed
else:
velocity.x = move_toward(velocity.x, 0, Speed)
velocity.z = move_toward(velocity.z, 0, Speed)
if Input.is_action_just_pressed("Move_Left"):
$Sprite3D.flip_h = true
$Sprite3D.offset.x = -9.0
$Hitbox/CollisionShape3D.position.x = absf($Hitbox/CollisionShape3D.position.x) * signf(velocity.x)
if Input.is_action_just_pressed("Move_Right"):
$Sprite3D.flip_h = false
$Sprite3D.offset.x = 9.0
if velocity == Vector3.ZERO:
idle = true
if idle == true and pickingup == false:
$AnimationPlayer.play("Idle_Unarmed")
static_electricityfunc() #function call
else:
idle = false
if idle == false and pickingup == false:
$AnimationPlayer.play("Walk_Unarmed")
if idle == true and pickingup == true or idle == false and pickingup == true:
$AnimationPlayer.play("PickUp_Unarmed")
await get_tree().create_timer(1.0).timeout
velocity = Vector3.ZERO
pickingup = false
$Cameracontroller/CameraManager/Camera3D/CanvasLayer/Label.text = str("Sticks =", sticks)
$Cameracontroller/CameraManager/Camera3D/CanvasLayer/Sprite2D3.frame = static_electricity
move_and_slide()
func static_electricityfunc(): #this function
static_electricity += 1
await get_tree().create_timer(1.0).timeout
The lightning will be added later