+1 vote

Script a object shooting arrow to player. At first use player's translation minus arrow's translation after set the same y, then normalize the results to get dir, using move_and_slide(dir * speed) to move the arrow, sometimes the arrow moving direction is right sometimes wrong. After doing some search it seems the translation is local, then change it to get_global_transform().origin, but the results is kind same as before. How to calculate that correctly?
Thanks!
edit: used look at to rotate the arrow, somehow it change the position of the arrow, so change it to rotate, problem solved

in Engine by (74 points)
edited by

1 Answer

0 votes
Best answer

var heading = (b.global_transform.origin - a.global_transform.origin).normalized()

by (5,247 points)
selected by

changed my script, but still the arrow shoot at position right to player, not exactly at player's position.

Is the mesh/sprite or another node of your player moved away from the player's origin and it just looks like that?

Thanks for your reply, found the arrow shooting base is moved, the transform -> translation property is edited to move the base and arrow is instanced as a child of the base. I set the property to (0, 0, 0) the direction is right. But how can I move the base and keep the arrow still shooting at right direction?

Basically it is physically correct when the speed of the parent is added to the arrow (until air draft comes into play). But that is not helpful here, I guess you don't want to have physically correct arrow behaviour which would make many things more complicated.

Simply move the arrow out of the parent. (i.e. instantiate as child of the scene root)
Just think about assigning the correct initial position (and or transform) to the arrow.

Try this add a new node (which base node of everithing) because this cant be local spaced.
Scene tree:
Base
--Node
----Arrow

Or trying move the arrow global position instead local
Or make the arrow should will be not the parent of base when firing

Sry for ma english.

Try to separate the arrow scene and the base scene, using signal to inform the main scene to instance arrow, tried to pass base's translation or global_transform.origin as a arg but the arrow didn't spawn at the base's location and the direction is wrong once the base is not at (0, 0, 0).

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.