raycast script to get group of the thing its colliding with not working
Asked Answered
B

5

0

im very new to coding and im trying to make a raycast that can detect what group the thing its colliding with is in, but it just doesnt seem to want to work. ive tried so many different tutorials but so far nothings worked. do you guys know what im doing wrong?

Berkeleian answered 11/1 at 1:25 Comment(0)
R
0

Berkeleian Post the entire code context in textual form.

Risky answered 11/1 at 1:33 Comment(0)
B
0

Risky alright but its the whole player script. the raycast bit is about a third of the way down

extends CharacterBody3D

class_name player

#variables and shit

@onready var headboy = $headboy
@onready var bigboycollider = $bigboycollider
@onready var smallboycollider = $smallboycollider
@onready var ray_cast_3d = $RayCast3D
@onready var interact_ray = $headboy/interact_ray


var lerp_speed = 10

var current_speed = 0
var mouse_sens = 0.2

var direction = Vector3.ZERO
var crouch_depth = -0.5
var lightup = false
var paused = false

#constants

const WALK_SPEED = 4.5
const JUMP_VELOCITY = 6
const SPRINT_SPEED = 8
const CROUCH_SPEED = 2.5

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")


#mouse input
func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	$headboy/Camera/flashliught.light_energy = 0
	$"../pausebutt".visible = false
	$"../pausebg".visible = false
func _input(event):
	if event is InputEventMouseMotion and paused == false:
		rotate_y(deg_to_rad(-event.relative.x * mouse_sens))
		headboy.rotate_x(deg_to_rad(-event.relative.y * mouse_sens))
		headboy.rotation.x = clamp(headboy.rotation.x,deg_to_rad(-89), deg_to_rad(89))
	
	
	

#checks for schtuff

func _physics_process(delta):
	
	if interact_ray.is_colliding():
		var collision = interact_ray.get_collider()
		
		if collision.is_in_group("collectible"):
			print("collectible")
	
	
	
	
	if Input.is_action_pressed("crouch"):
		current_speed = CROUCH_SPEED
		headboy.position.y = lerp(headboy.position.y,0.519 + crouch_depth, delta*lerp_speed)
		bigboycollider.disabled = true
		smallboycollider.disabled = false
	elif !ray_cast_3d.is_colliding():
		bigboycollider.disabled = false
		smallboycollider.disabled = true
		
		headboy.position.y = lerp(headboy.position.y,0.519, delta*lerp_speed)
		if Input.is_action_pressed("sprint"):
			current_speed = SPRINT_SPEED
		else:
			current_speed = WALK_SPEED
	
	
	
	
	#flashlight toggle shits
	
	if Input.is_action_just_pressed("flashlight") and lightup == false:
		$headboy/Camera/flashliught.light_energy = 0.835
		$headboy/Camera/flashliught/clickon.play()
		lightup = true
		print("on")
	elif Input.is_action_just_pressed("flashlight") and lightup == true:
		$headboy/Camera/flashliught.light_energy = 0
		$headboy/Camera/flashliught/clickoff.play()
		lightup = false
		print("off")
	
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta
	
	#pause toggle shits
	
	if Input.is_action_just_pressed("escape") and paused == false:
		$"../pausebutt".visible = true
		$"../pausebg".visible = true
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		paused = true
		print("pause")
	elif Input.is_action_just_pressed("escape") and paused == true:
		$"../pausebutt".visible = false
		$"../pausebg".visible = false
		paused = false
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		print("unpause")


	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		print("jumpy guy")

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir = Input.get_vector("left", "right", "forward", "backward")
	direction = lerp(direction, (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized(),delta*lerp_speed)
	if direction:
		velocity.x = direction.x * current_speed
		velocity.z = direction.z * current_speed
	else:
		velocity.x = move_toward(velocity.x, 0, current_speed)
		velocity.z = move_toward(velocity.z, 0, current_speed)

	move_and_slide()



func _on_back_pressed():
	$"../click".play()
	FadeTransition.change_scene_to_file("res://menu2/mainmenu2.tscn")
Berkeleian answered 11/1 at 1:41 Comment(0)
R
0

Berkeleian First check if is_colliding() ever returns anything that is not null. If not, the problem is likely with raycast node setup. Enable debug display of collision objects in editor and look if the ray has adequate length and direction.

Risky answered 11/1 at 1:51 Comment(0)
B
0

Risky alright ill let you know what i find

Berkeleian answered 11/1 at 1:54 Comment(0)
B
0

i didnt change anything but i reloaded the project file and it started working all the sudden. idk what that was about lol.

thanks for the help tho : D

Berkeleian answered 11/1 at 2:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.