0
\$\begingroup\$

I'm trying to create an isometric game with Diablo-like control, but gravity isn't working. The CharacterBody3D won't touch the floor, the print(is_on_floor()) always results false.

Here's my code for PlayerCapsule.gd

extends ActorPlayable3D
class_name PlayerCapsule


var navigationAgent3D

func _ready():
    navigationAgent3D = get_node(navigationAgent)


func _physics_process(delta):
    velocity.y -= gravity * delta
    if navigationAgent3D.is_navigation_finished():
        return
    
    var movement_delta = speed * delta
    var next_path_position: Vector3 = navigationAgent3D.get_next_path_position()
    var current_agent_position: Vector3 = global_position
    var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * speed
    if navigationAgent3D.avoidance_enabled:
        navigationAgent3D.set_velocity(new_velocity)
    else:
        _on_navigation_agent_3d_velocity_computed(new_velocity)

func setNavigationAgentToLocation(pos: Vector3):
    navigationAgent3D.set_target_position(pos)


func _on_navigation_agent_3d_velocity_computed(safe_velocity):
    velocity = safe_velocity
    print(safe_velocity)
    print(is_on_floor())
    move_and_slide()

And here's the ActorPlayable3D.gd

extends CharacterBody3D
class_name ActorPlayable3D

@export var speed: float = 5.0
@export var navigationAgent: NodePath

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

class Navigation:
    func setNavigationAgent(node: NavigationAgent3D) -> void:
        pass

And here's the image result of the output, and playable scene:

enter image description here

enter image description here

How can I fix the gravity and make the player touch the floor?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

For is_on_floor to return true, the body needs to be moving (e.g. move_and_slide was called) against a floor (e.g. with gravity applied).

But the next position according to navigation is not below the ground, so the new velocity you compute is not going to downwards. And thus, the body would not be falling.

Ergo, you need to apply gravity after navigation.

A lesser issue is that the result of is_on_floor is updated when you call move_and_slide, so if you check it before move_and_slide you are getting the result from last physics frame. Which might or might not be what you want.

\$\endgroup\$
2
  • \$\begingroup\$ Problem has been solved in this Link \$\endgroup\$ Jun 14, 2023 at 17:59
  • \$\begingroup\$ I requested a mirror in case somebody wants to look at it, and for whatever reason the site is down: Link. \$\endgroup\$
    – Theraot
    Jul 21, 2023 at 22:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .