How do I rotate a single point(vector3) so that it is facing towards the player

30 Views Asked by At

Quick explanation: I am trying to design a system where I get a random point from a circle and I managed to create the random point and I spawn a mesh cube for me to visually see it working, but the issue I am having is that when I decide to rotate off the forward axis (-z) the point does not rotate, I have attempted to rotate it like so.

rand_point = rand_point.rotated(Vector3.RIGHT,player.rotation.y)

unfortunately this does not work, im not good at dealing with rotations.

Code:

extends Node

@export_subgroup("Misc")
@export var player : CharacterBody3D
@export var raycast : RayCast3D
@onready var bullet_impact : PackedScene = preload("res://Source/bullet impact/bullet_impact.tscn")
@export_subgroup("Temp paremeters")
@export var fire_rate : float = 0.1
var fire_time : float = 0.0

@export_subgroup("Recoil parameters")
var radius : int = 1
var impact_array : Array

func _process(delta):
    
    if Input.is_action_just_pressed("Clear"):
        clear_impact()
    
    if fire_time > 0.0:
        fire_time -= delta
    
    if Input.is_action_pressed("Fire") and fire_time <= 0.0:
        fire_time = fire_rate
        handle_raycast()
    
func handle_raycast() -> void:
    var rand_point : Vector3 = Vector3.ZERO
    
    if raycast.is_colliding():
        var obj = raycast.get_collider()
        var point = raycast.get_collision_point()
        var normal = raycast.get_collision_normal()
        
        rand_point = get_rand_point_on_circle(point)
        rand_point = rand_point.rotated(Vector3.RIGHT,player.rotation.y)
        handle_impact(rand_point)

func clear_impact() -> void:
    if impact_array.size() == 0: return
    
    for impact in impact_array:
        if is_instance_valid(impact):
            impact.queue_free()

func handle_impact(impact_pos : Vector3) -> void:
    if impact_pos == Vector3.ZERO: return
    var impact = bullet_impact.instantiate()
    get_tree().current_scene.add_child(impact)
    impact.global_position = impact_pos
    impact_array.append(impact)


func get_rand_point_on_circle(center : Vector3) -> Vector3:
    var theta = randf_range(0, 2 * PI)
    
    # Generate random distance from the center (within the circle's radius)
    var distance = randf_range(0, radius)
    
    # Calculate the position of the random point in the plane of the circle
    var x = center.x + distance * cos(theta)
    var y = center.y + distance * sin(theta)
    
    # Z-coordinate remains the same as the center_point's z-coordinate to keep the point flat
    var z = center.z
    
    return Vector3(x, y, z)

normal - forward direction

enter image description here

off to the side

enter image description here

another angle

enter image description here

0

There are 0 best solutions below