Cannot get collision point of raycast properly

Godot Version

v4.2.2.stable.official [15073afe3]

Question

Hi! I’m kind of new to godot and programming in general. I wanted to make a weird movement system where you use the arrow keys to move around, but the difference is you can’t hold it down. The problem isn’t that. The problem is, when i try to cast a raycast3d down to the ground and get the collision poiny, and then snap the character to that point, it says "Invalid get index ‘collision_point’ (on base:‘Nil’). Here is the code for the movement and the raycast3d.

Movement

extends CharacterBody3D
@onready var characterbody3d = $"."
var stepdistance = 0.5
var turndegree = 10
@onready var camera_3d = $Camera3D
var facing_direction = 0
@onready var raycast3d = $CollisionShape3D/RayCast3D
@onready var bottom = $".."
var collision_point = raycast3d.collision_point        #the error is here.
# Called when the node enters the scene tree for the first time.
func _ready():
	facing_direction = camera_3d.rotation.y

func _process(_delta):
	
	if Input.is_action_just_pressed("left"):
		facing_direction += deg_to_rad(turndegree)
		camera_3d.rotation.y = facing_direction
	if Input.is_action_just_pressed("right"):
		facing_direction -= deg_to_rad(turndegree)
		camera_3d.rotation.y = facing_direction
	if Input.is_action_just_pressed("up"):
		var forward_direction = camera_3d.transform.basis.z.normalized()
		camera_3d.position -= forward_direction * stepdistance
		bottom.position.y = Vector3(0, collision_point, 0)


Raycast3D

extends RayCast3D
@onready var raycast3d = $"."

# Called when the node enters the scene tree for the first time.
func _ready():
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	if raycast3d.is_colliding():
		var collision_point = raycast3d.get_collision_point()

So what I think is happening, is that there is no collision when you are setting the variable.

Is there always going to be ground? If so, try use @onready var collision_point = raycast3d.get_collision_point() instead.

Otherwise, try:

var collision_point : Vector3


func _process(_delta):
	if raycast3d.is_colliding():
		collision_point = raycast3d.get_collision_point()

Spoilers: I am new myself, so I may be wrong.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.