···11+extends Camera3D
22+33+44+# Called when the node enters the scene tree for the first time.
55+func _ready() -> void:
66+ pass # Replace with function body.
77+88+99+# Called every frame. 'delta' is the elapsed time since the previous frame.
1010+func _process(delta: float) -> void:
1111+ pass
···13131414@onready var camera_point: Node3D = $camera_point
15151616+@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D
1717+1818+1619var walking = false
1720var sprinting = false
1821···2831 animation_player.set_blend_time("walk", "run", 0.2)
2932 animation_player.set_blend_time("run", "walk", 0.2)
30333434+func moveToPoint(delta, speed):
3535+ var targetPos = navigation_agent_3d.target_position
3636+ var direction = global_position.direction_to(targetPos)
3737+3838+ velocity = direction * speed
3939+ visuals.look_at(direction + position)
4040+4141+ move_and_slide()
4242+3143func _physics_process(delta: float) -> void:
3232-3344 # Add the gravity.
3445 if not is_on_floor():
3546 velocity += get_gravity() * delta
3636-3737- # Handle jump.
3838- if Input.is_action_just_pressed("jump") and is_on_floor():
3939- velocity.y = JUMP_VELOCITY
4040-4141- # Get the input direction and handle the movement/deceleration.
4242- # As good practice, you should replace UI actions with custom gameplay actions.
4343- var input_dir := Input.get_vector("left", "right", "forward", "backward")
4444- var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
4545- if direction:
4646- var speed = WALKING_SPEED
4747- if Input.is_action_pressed("sprint"):
4848- sprinting = true
4949- speed = speed * SPRINT_MULT
5050-5151- velocity.x = direction.x * speed
5252- velocity.z = direction.z * speed
53475454- visuals.look_at(direction + position)
5555- if sprinting:
5656- sprinting = false
5757- animation_player.play("run")
5858- else:
5959- walking = true
6060- animation_player.play("walk")
4848+ var speed = WALKING_SPEED
4949+ if (navigation_agent_3d.is_navigation_finished()):
5050+ animation_player.play("idle")
5151+ return
5252+5353+ # Actual moving
5454+ if (navigation_agent_3d.distance_to_target() > 10):
5555+ animation_player.play("run")
5656+ speed = speed * SPRINT_MULT
6157 else:
6262- velocity.x = move_toward(velocity.x, 0, WALKING_SPEED)
6363- velocity.z = move_toward(velocity.z, 0, WALKING_SPEED)
5858+ animation_player.play("walk")
5959+ speed = WALKING_SPEED
6060+ moveToPoint(delta, speed)
6161+6262+6363+ ## Get the input direction and handle the movement/deceleration.
6464+ ## As good practice, you should replace UI actions with custom gameplay actions.
6565+ #var input_dir := Input.get_vector("left", "right", "forward", "backward")
6666+ #var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
6767+ #if direction:
6868+ #if Input.is_action_pressed("sprint"):
6969+ #sprinting = true
7070+ #
7171+ #
7272+ #velocity.x = direction.x * speed
7373+ #velocity.z = direction.z * speed
7474+ #
7575+ #
7676+ #if sprinting:
7777+ #sprinting = false
7878+ #
7979+ #else:
8080+ #walking = true
8181+ #animation_player.play("walk")
8282+ #else:
8383+ #velocity.x = move_toward(velocity.x, 0, WALKING_SPEED)
8484+ #velocity.z = move_toward(velocity.z, 0, WALKING_SPEED)
8585+ #
8686+ #if walking:
8787+ #walking = false
8888+ #sprinting = false
8989+ #animation_player.play("idle")
9090+ #
9191+ #sprinting = false
9292+ #move_and_slide()
9393+9494+func _input(event: InputEvent) -> void:
9595+9696+ # Move character
9797+ if Input.is_action_just_pressed("leftMouse"):
9898+ # Setup
9999+ var camera = get_node("camera_ray_tracing")
100100+ var mousePos = get_viewport().get_mouse_position()
101101+ var rayLength = 100
102102+ # We cast a ray from the mouse position on the screen (of the camera)
103103+ var from = camera.project_ray_origin(mousePos)
104104+ # to a point 100 units forward perpendicular to the camera
105105+ var to = from + camera.project_ray_normal(mousePos) * rayLength
106106+ # We get the state of the space which we are going to ray-query
107107+ # If a ray is a query to get a coordinate, the space state is the BDD
108108+ var space = get_world_3d().direct_space_state
109109+ # We instantiate a ray query, load it up and fire it
110110+ var rayQuery = PhysicsRayQueryParameters3D.new()
111111+ rayQuery.from = from
112112+ rayQuery.to = to
113113+ rayQuery.collide_with_areas = true
114114+ var result = space.intersect_ray(rayQuery)
641156565- if walking:
6666- walking = false
6767- sprinting = false
6868- animation_player.play("idle")
116116+ # We tell the navigation agent to travel to the queried position
117117+ navigation_agent_3d.target_position = result.position
118118+ var debug_pos = "Navigating to %s"
119119+ print(debug_pos % result.position)
691207070- sprinting = false
7171- move_and_slide()
121121+