Godot Version
4.4.1
Question
So in the image here my raycast is in the bottom left when it’s supposed to be in the center of the character i made. I’m using a line to draw the raycast and I’m not sure what’s happening. (also i drew the cursor on where it was approximately as i cant screenshot my cursor)
This is the code in the RayCast 2D:
extends RayCast2D
@onready var ray = $"."
@onready var player = $"../player"
@onready var debugLine = $"../Line2D"
var mechTR = load("res://Assets/Sprites/MechClops/mechClopsTR.png")
var mechTL = load("res://Assets/Sprites/MechClops/mechClopsTL.png")
var mechBR = load("res://Assets/Sprites/MechClops/mechClopsBR.png")
var mechBL = load("res://Assets/Sprites/MechClops/mechClopsBL.png")
func _physics_process(_delta: float):
# Get the mouse position in global coordinates
var mouse_pos = get_viewport().get_mouse_position()
ray.target_position = mouse_pos
set_enabled(true)
if target_position.x >= 600 and target_position.y <= 450:
player.texture = mechTR
elif target_position.x >= 600 and target_position.y >= 450:
player.texture = mechBR
elif target_position.x <= 600 and target_position.y <= 450:
player.texture = mechTL
elif target_position.x <= 600 and target_position.y >= 450:
player.texture = mechBL
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
ray.collide_with_areas = true
if ray.is_colliding():
var raycollider = ray.get_collider()
print(raycollider.name)
ray.collide_with_areas = false
#if is_colliding():
# var collider = get_collider()
#
# print("AAAAAA: " + collider.name)
func _on_draw() -> void:
ray.draw_line(Vector2(640, 480), Vector2(ray.target_position), Color.AQUA, -1, false)