Drawing 2D line from character towards mouse but for specific maximum length

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

I wish to draw weapon attack range indicator from character to mouse but with melee weapons I need to have shorter line length…

Either I would need formula to shorten it to manually defined length or even better calculate length based on bullet speed and lifetime.

Currently using code below.
draw_line(hero.position, get_local_mouse_position(), Color(255, 0, 0, 0.5), 3, true)

:bust_in_silhouette: Reply From: Lopy

You can calculate a normal a normal (length 1) Vector2, and multiply it by the wished length. That would give you:

var normalized = (get_local_mouse_position() - hero.position).normalized()
var target = hero.position + normalized * length
drawline(hero.position, length, Color(255, 0, 0, 0.5), 3, true)

For your projectiles, if you have speed in distance/time, lifetime a,d want to calculate distance, just multiply speed with lifetime. You may be off by a constant factor depending on units mismatch, notably if speed is in distance/tick length. Try multiplying/divinding by delta.

perfect, thank you !

wildboar | 2021-02-14 07:59

In line:
drawline(hero.position, length, Color(255, 0, 0, 0.5), 3, true)

There should be “target” instead of “length”

sporx13 | 2022-01-09 11:28