Turning character using radians/degrees and raycast to 2dMarker

Godot Version

Godot Engine v4.2.2.stable.official.15073afe3

Question

Good day community. I’m want to make my character in top down game to turn (trigger right/left/top or down animation) by clicking a button at the screen. Depending on the angle of raycast that always point at the center of other polygon collider (i put a 2dmarker inside) from my character. And i have no idea how to write it down. Or even get the angle right.
I have my player position and a collision point. But don’t know how to use it. Can somebody help me please?

p.s. Generally - I’m asking how set starting vector to the right. And then measure angle between starting vector and raycast vector.

extends CharacterBody2D

@onready var player = $"."
@onready var my_target = $"../myTarget"
@onready var area_2d = $RayCast2D/Area2D

@onready var ray_cast = $RayCast2D


var colpoint = preload("res://scenes/colpoint.tscn")

const SPEED = 300.0


func _physics_process(delta):

	var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	velocity = direction * SPEED
	move_and_slide()
	ray_cast.look_at(my_target.position)
	

func _on_button_pressed():
	#ray_cast.look_at(my_target.position)
	if ray_cast.is_colliding():
		var playerPos = player.global_position
		var colPoint = ray_cast.get_collision_point()
		print(ray_cast.get_collider())
		print("collision point " + str(colPoint))
		print("player position x, y " + str(playerPos))
		
		print(colPoint.from_angle(0).angle_to(colPoint))
		#var angle = playerPos.angle_to(colPoint)
		#print("angle is: " + str(rad_to_deg(angle)))
	else:
		print("No collider found!")
	

this code gives me strange numbers :unamused:

Solved!
I used: “var playerPos = player.global_position”
instead of: “var playerPos = player.position”.
Not it’s working right.

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