Godot Engine v4.5.stable
Question
After looking into different post and codes, I finally managed to have the player character moves to location, using the left mouse button.
Problem, the character is now jittering when it reaches the location.
I believe there is something I forgot or that I need to simplify in order to prevent the script from going back and forth when reaching the POS.
Current Code
extends CharacterBody3D
var gravity = 9.8
@export var speed = 5.0
@export var camera : Camera3D
@export var navigation_agent : NavigationAgent3D
func _physics_process(delta):
if not is_on_floor():
velocity.y -= gravity * delta
var new_velocity = movement()
velocity.x = new_velocity.x;
velocity.z = new_velocity.z;
move_and_slide()
func movement():
var next_pass_position = navigation_agent.get_next_path_position()
var current_agent_position = global_position
var new_velocity = next_pass_position - current_agent_position
new_velocity = new_velocity.normalized() * speed
return new_velocity
func _input(_InputEvent):
if Input.is_action_just_pressed(“click_move”):
get_action_pos();
func get_action_pos():
var mouse_pos = get_viewport().get_mouse_position();
var ray_length = 100
var from = camera.project_ray_origin(mouse_pos)
var to = from + camera.project_ray_normal(mouse_pos) * ray_length
var space = get_world_3d().direct_space_state
var ray_query = PhysicsRayQueryParameters3D.new()
ray_query.from = from;
ray_query.to = to;
var result = space.intersect_ray(ray_query);
navigation_agent.target_position = result.position;
look_at_path(result.position)
func look_at_path(direction : Vector3):
look_at(Vector3(direction.x, global_position.y, direction.z), Vector3.UP)
Note
I’m very new, but learning bits by bits.
I believe it is something going in the “func movement():” that is undoing the normalization and cause it to move at a Velocity using a floating value.