#Arrow

1 messages · Page 1 of 1 (latest)

wide portal
#

if you know the x and y coordinates of the point you want your arrow to point at you just do

let your_x = ...
let your_y = ...
let target_x = ...
let target_y = ...

// relative to your character
let x = target_x - your_x
let y = target_y - your_y

// calculate distance with pythagoras
let distance = sqrt(pow(x, 2) + pow(y, 2))

// get the relative distance into a range of 0 to 1 or 0% to 100%
let percent_x = x / distance
let percent_y = y / distance

let arrow_on_screen_x = screen.width / 2 + percent_x * screen.width / 2
let arrow_on_screen_y = screen.height / 2 + percent_y * screen.heigh / 2
#

you could also do this with angles via sin and cosine

#

this just scales everything down

north breach
#

👍

north breach
#

I've restarted Godot several times and I'm still getting this error when I try to reference a node in the arrow script

dawn knoll
#

Show your entire Arrow script?

north breach
wet cloak
#

do you still get the error once you change the line to "@onready var target : StaticBody2D = null"

north breach
wide portal
wet cloak
# north breach The error comes up when I try to drag in another node to reference it.

Yeah you shouldn't be able to drag it in to the script itself, because the class exists independent of the tree structure until initialized. In other words instead of telling your dog Fido to find your cat Scritches, you are telling dogs in general to find Scritches before the big bang, which doesn't make much sense.

You have 4 options

  1. Do what Priton said

  2. Access the node using $ syntax (get_node shorthand)

  3. Access the node using % syntax (get_node shorthand for scene unique nodes)

  4. If you are actually trying to access a class/script and not a node/instance, instead write "@onready var target = preload()" and drag the script into the parentheses