3D RTS Click to move, Raycast manually Godot 3.5.1

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

Hi there i have trouble with ray_cast
i want the character to move to the target
i am trying to ray_cast manually

First is camera ray casting

extends Spatial

onready var cam = $Camera

const ray_length = 1000

var space_state : PhysicsDirectSpaceState

func _input(event):

if event as InputEventMouseButton == null:
	return
	
if event is InputEventMouseButton:
	if event.button_index == BUTTON_RIGHT:
		if event.pressed:
			space_state = get_world().direct_space_state
			var start = cam.project_ray_origin(event.position)
			var end = start + cam.project_ray_normal(event.position) * ray_length
			var result = space_state.intersect_ray(start, end, [], 1)
			if result:
			    	$"../Marker".transform.origin = event.position
                                $unit.target = event.positin
			print(result)

This is the unit, the target that has to move

extends KinematicBody

export var speed = 5
export var gravity = -5

var target = null
var velocity = Vector3.ZERO

func _physics_process(delta):
velocity.y += gravity * delta
if target:
	look_at(target, Vector3.UP)
	rotation.x = 0
	velocity = -transform.basis.z * speed
	if transform.origin.distance_to(target) < .5:
		target = null
		velocity = Vector3.ZERO
velocity = move_and_slide(velocity, Vector3.UP)

Edited to fix code formatting…

jgodfrey | 2023-01-10 15:08

Thanks for the edit jgodfrey

BooneTo | 2023-01-10 18:22