This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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.
drawline(hero.position, getlocalmouseposition(), Color(255, 0, 0, 0.5), 3, true)

in Engine by (87 points)
edited by

1 Answer

0 votes
Best answer

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.

by (2,720 points)
selected by

perfect, thank you !

Drawline works, how can I make it work with raycast2d ?

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

There should be "target" instead of "length"

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.