lukky-nl / FPS-controller-assets

19 stars 9 forks source link

the compleat inability to jump #3

Open allen20010405 opened 1 month ago

allen20010405 commented 1 month ago

I have followed your beginner tutorial on the fps controller, however, the jump is now limited to a tiny hop, I think it might be because of the lerp() function used, so i am requesting you have a look at this code plez

extends CharacterBody3D

@export var current_speed = 5.0 @export var corrent_jumpV = 4.5

@onready var head = $head

@export var speed = 5.0 @export var sprinting_speed = 10.0 @export var crouching_speed = 3.0 @export var jump_velocity = 4.5 @export var sprinting_jumping_velocity = 3

sets amounts oftime you can jump while in the air, 0 will mean player can jump once

@export var Double_jump = 3 var Double_jump_delta = Double_jump @export var mouse_sense = 0.4 @export var lerp_speed = 10.0

var direction = Vector3.ZERO

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _ready(): Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event): if event is InputEventMouseMotion: rotate_y(deg_to_rad(event.relative.x -mouse_sense)) head.rotate_x(deg_to_rad(event.relative.y -mouse_sense)) head.rotation.x = clamp(head.rotation.x,deg_to_rad(-98),deg_to_rad(98))

func _physics_process(delta):

if Input.is_action_pressed("sprint"):
    current_speed = sprinting_speed
else:
    current_speed = speed

# Add the gravity.
if not is_on_floor():
    velocity.y -= gravity * delta

# Handle jump.
if Double_jump_delta == 0:
    if Input.is_action_just_pressed("jump") and is_on_floor():
        velocity.y = corrent_jumpV
        Double_jump_delta = Double_jump

else:
    if Input.is_action_just_pressed("jump"):
        velocity.y = corrent_jumpV
        Double_jump_delta -= 1

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("left", "right", "forward", "backward")
direction = lerp(direction,(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized(),delta * lerp_speed)
if Input.is_action_pressed("exit"):
    Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if direction:
    velocity.x = direction.x * current_speed
    velocity.z = direction.z * current_speed
else:
    velocity.x = move_toward(velocity.x, 0, current_speed)
    velocity.z = move_toward(velocity.z, 0, current_speed)

move_and_slide()
allen20010405 commented 1 month ago

sorry about the format