How to make RigidBody3D move forwards based on it's orientation?

Godot Version

4.2.2 (Steam)

Question

I am trying to make a 3D projectile weapon that constantly moves forward using linear_velocity, but it always moves in one direction, regardless of it’s orientation. My code to move the bullet is:func _ready(): linear_velocity = Vector3(0,0,-velocity)
It only moves backward on the global z-axis, but I need it to move based on the orientation it is facing. I swear I remember there being a function for that for Vector3, but I can’t remember. Can anyone help?

Try:

linear_velocity = global_transform.basis * Vector3(0,0,-velocity) 

Hmm… When I try that it moves positively along the x-axis.

Then you probably aren’t rotating the body correctly, that’s the code to make it move based on it’s orientation, if you instead have the orientation as a value somewhere else you need to show your code for that

Yeah I thought it might be that too, but in game, the bullet is a rectangle shape and it’s orientation is correct, as it’s different depending on how the camera is rotated as it shoots. However, regardless of how it is rotated, it moves positively along the x-axis.

Ok actually, I just tried func _physics_process(delta): position += global_transform.basis * Vector3(0,0,-velocity) , and that worked fine, but I would prefer using linear_velocity because then it will properly interact with physics objects.

You need to update the velocity constantly or it won’t react to movement, you didn’t make it clear it rotates as it updates

Oh thanks, that works! I simply moved the code from _ready to _physics_process() and it works! I don’t know how I didn’t try that initially.

1 Like