Is_on_floor is absolutely not working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TheNewbie360

is_on_floor always returns the value False, even though its colliding with my floor.
Move_and_slide always returns the value (0, 0, 0) when the character stands still

Here the complete code of my Character:

extends KinematicBody

var velocity = Vector3()
var direction = Vector3()
var acceleration = 12
var mouse_sensity = 0.1
const SPEED = 6
const gradlol = 6
export var gravity = 70
export var jumpstrength = 20
var confirm = 0

onready var pivot = $Pivot

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

func _input(event):
if Input.is_action_just_pressed(“ui_cancel”):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if event is InputEventMouseMotion:
rotate_y(deg2rad(-event.relative.x * mouse_sensity))
pivot.rotate_x(deg2rad(-event.relative.y * mouse_sensity))
pivot.rotation.x = clamp(pivot.rotation.x, deg2rad(-90), deg2rad(90))

func _physics_process(delta):

apply_gravity(delta)
jump()
velocity = move_and_slide(velocity, Vector3.UP)

if Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_right"):
	direction.x = 0
elif Input.is_action_pressed("ui_right"):
	direction = transform.basis.x
	$MeshInstance.rotate_z(deg2rad(-gradlol))
elif Input.is_action_pressed("ui_left"):
	direction -= transform.basis.x
	$MeshInstance.rotate_z(deg2rad(gradlol))
else:
	direction.x = 0

if Input.is_action_pressed("ui_up") and Input.is_action_pressed("ui_down"):
	direction.z = 0
elif Input.is_action_pressed("ui_up"):
	direction -= transform.basis.z
	$MeshInstance.rotate_x(deg2rad(-gradlol))
elif Input.is_action_pressed("ui_down"):
	direction = transform.basis.z
	$MeshInstance.rotate_x(deg2rad(gradlol))
else:
	direction.z = 0
	


direction = direction.normalized()
velocity = direction * SPEED
velocity.linear_interpolate(velocity, acceleration * delta)
print(move_and_slide(velocity))

func apply_gravity(delta):
velocity.y -= gravity
move_and_slide(velocity)
print(is_on_floor())

func jump():
if is_on_floor() and Input.is_action_pressed(“jump”):
velocity.y = jumpstrength

func _on_enemy_body_entered(body):
if body.name == “Character”:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
get_tree().change_scene(“res://Assets/Game Over.tscn”)

:bust_in_silhouette: Reply From: DaddyMonster

In future could you try to format the code using the curly braces above please? It’s very hard to read otherwise.

You need to tell the physics engine which way is up:

move_and_slide(velocity, Vector3.UP)

Hi, thanks for the answer. It works now. But I do have another problem now. My Character now teleports up instead of moving up like a normal jump and the jump strength of the character is also kinda low, even though it’s on 20. Could you help me once again?

TheNewbie360 | 2022-08-20 12:17

move_and_slide() calls the physics engine and tells it to calculate the physics for that frame but you’re doing it three times every frame - at the beginning of your physics_process method, inside a print, inside your apply_gravity method. Big no-no, just call it once (at the end after you’re done calculating velocity is usual).

You’ve got a normalised Vector3 direction which is setting the velocity with the scalar speed but in this line you directly set its y to 20:

velocity.y = jumpstrength

but with gravity you deduct 70:

velocity.y -= gravity

The “right way” depends on the outcome you want, but usually you add and minus from the velocity to get the nett velocity, rather than just directly set it:

velocity.y += jumpstrength

and

velocity += direction * SPEED

Also, your jump force needs to be greater than gravity otherwise you won’t go up.

This line here is super weird:

velocity.linear_interpolate(velocity, acceleration * delta)

I don’t know what this is trying to do. Lerping is for finding straight line intermediate values between zero and one, I can’t see any sense to this here. I’d delete this whole line if I were you.

DaddyMonster | 2022-08-20 14:53