How to make a light beam follow the player?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By leegf

Hi Everyone!
I’m new here, so… I got torubles lol
I’m a studen at university and i gotta do a game for my final (best thing ever S2), everything was going quite smoothly but now i’m in a pinch
I’ve tried and look, and google, and going to the bottom of internet to find a way to make the magic beam leaves a staff, from a boss in constant and random movement, and collides to the player, whatever his position is.

I really wish I could show my code raws, but they’re just crazy mess.
u can help me with tutorials links, youtube links or other crazy but more functional ideas!!
This projet is looking awesome, really but i got a tight deadline (06/09) (my cpu died and i’m currently at my bf notebook, w/o a lot of my work)

Thank you !
(people who can write in spanish I aprecciate, i know english but spanish is a lot easier - i’m brazilian)

:bust_in_silhouette: Reply From: muratbozkurt35

there is a function called look_at in transform class, you can use that i believe.

first calculate the distance between tip of staff and the player
then create your “beam” rectangle
then place that rectangle to tip of staff and set transform of that rectangle with a transform that looked player.

you may have to play with x and y values of location of that beam but i think this is da way.

God, I really thought this would work but maybe it’s cause my code is a basic mess, i’m trying simply everything i see on internet… in my head the code works, but reality not so much

In the boss scene, I’m trying to make the bullet locate the player, I tried figuring out what u sugested (“calculate the distance”), but if my player is constantly moving, and so is the Boss, how do I calculate this? LoL

This is my mess, I don’t know if the problem is I’m using a Static Body or not, or everthing else
(it keeps appearing the null instance error for global_position)

`extends StaticBody2D

var speed = 100
onready var player = preload(“res://Player.tscn”).instance()

func _ready():
set_process(true)

func _process(delta):
shoot()

func shoot():
get_parent().get_node(“.”).add_child(player)
var shot = preload(“res://Beam.tscn”).instance()
var direction = (look_at(player.global_position) - $muzle.global_position).normalized()
shot.global_position = $muzle.global_position
shot.dir = Vector2(cos(rotation), sin(rotation)).normalized()
get_parent().get_node(“.”).add_child(shot)`

leegf | 2020-06-08 06:08