Rotating a projectile to point a direction

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

Im adding ne projectiles to my game, but found a problem. With larger projecties, we noticed that the are not properly rotated towards the moving direction. That was not visible when using a small capsule, but if we use a larger particle system with a trail and a head, you see how the fireball moves with head to a side or any other direction.

To instantiate the projectile I use a node in the weapon scene and this code:

var owner_forward = owner.get_actor().global_transform.basis.z #forward dir of plyer model

var forward

if item_mesh!=null:

	forward = item_mesh.global_transform.basis.z	#fwd dir of weapon model


	forward = owner_forward #if weapon has no model, we use player model

trace.global_transform.origin = item_mesh.find_node("flash").global_transform.origin + owner_forward*1.5

trace.transform.basis.z = item_mesh.find_node("flash").global_transform.basis.z 

trace.dir = Vector2(owner_forward.x, owner_forward.z) #movement direction of bullet

How can I make my projectile move in the direction the player is facing and also rotate to cpoint tat direction?

Does your projectile always move in a straight line
Or sometimes curve on the y axis based on weight and distance?

If possible can you show a picture of the problem and or intended result?

Wakatta | 2022-09-14 22:13

It moves in straight line during 3 seconds, then I destroy it. The movement works well, the problem is that I cant use different projectiles or projectiles with trails.

rogerdv | 2022-09-15 17:29

:bust_in_silhouette: Reply From: Wakatta
#movement direction of bullet

var origin = global_transform.origin
var target = projectile_target.global_transform.origin
trace.dir = get_rotation_degrees(origin.direction_to(target))

if your projectile has homing abilities place the above in _process to keep updated