As the title says, I need to know how can I make an object move forward (according with direction of it), cause I didn't found nothing like that on the docs (maybe lack of search)

Can you give more information ? Are you working on a 2D game ? Is it a top down view ?

EDIT: func _fixed_process(delta): var speed


	if Input.is_key_pressed(KEY_W):
		speed = 4
	else:
		speed = 0
		
	translate(Vector3(0,0,delta * speed))

you should include 2D or 3D in your title. I assume this is 3D though since if you cannot find help in 2D, you probably do not have a fully functioning brain(that stuff accounts fro 95% of all godot info...absolutely oversaturated with 2D info 3D is a wasteland)..it is in the documentation but someone could at least give some info ;)

I know crude, but I wanted to break it down to bare bones just to get a reaction and then go from there

...I notice threads here get a lot of views and nearly no replies...while the replies are relatively warm, people here could help a bit more. EDIT: I can be dense, I realize now that the high view to response ratio is due to people looking for answers...

"That's right 6,000 hulls"

This is a good topic! :) Though the initial question is a bit unspecific...

I am currently programming in the 3D "world". Therefore I try to write some info about 3D movement. Generally, I am thinking about posting a 3D Vector and Matrix "Cheat Sheet" thread in this forum. To be (similar to the previous thread) open for corrections and additions.

Movement in 3D in its most basic form can be done (All Descendands of Spatial) with: translate(Vector3(x,y,z))

Other methods are i.e. to use apply_impulse(local_force_pt,force_vector) which is helpful for boats/aircraft/spaceships on i.e. RigidBody physics.

Direction: Your key for direction might be: get_transform().basis (for location rotation direction) or get_global_transform().basis (for global rotation direction)

Most of the time, you might want to use the global rotation direction.

If your object "looks" forward in the direction of its local z-axis then the forward vector should be: get_global_transform().basis.z.normalized()

Left/Right movement along "basis.x" Up/Down movement along "basis.y"

I'm unsure if "normalized" is required here. It sets the vector to a total length of 1 which basis vectors usually already have. Also I'm unsure if a "* -1" is required as forward Z-Axis is usually negative. So, if your object moves backwards, multiply the vector with -1.

Moving an object could be done along this vector. This code is completely untested, maybe it works ;-):

func _fixed_process(delta):
		#10 km/h -> m/s
		var metersPerSecond = 10 / 3.6 

		# multiply by delta to apply the fraction of a second of _fixed_process 
		# (all deltas of all _fixed_process calls during 1 second add up to 1)
		translate(get_global_transform().basis.z.normalized() * delta * metersPerSecond)

If you're working with RigidBody and moving/gliding objects you may want to apply an impulse instead of using translate():

apply_impulse(Vector3(0,0,0), get_global_transform().basis.z.normalized() * delta * thrustValue)

"thrustValue" is a variable which should hold the amount of the force to apply. The unit of that value is probably "newton metre".

P.S.: I looked it up and set_linear_velocity() is available on RigidBody objects. But it should not be called in _fixed_process. Probably it is more useful to be set in a keyboard event handler or something similar.

The call would per something like that: set_linear_velocity(get_global_transform().basis.z.normalized() * metersPerSecond) Notice the missing "delta" here. This is because you set a desired "speed" and not a location increment as in "translate" or a repeated impulse like in "apply_impulse".

I initially did use translate, but when the movement was not in local space I tried linear_velocity...and just never got any further, the "basis" thing...is not documented that I can tell...is that localized? I am confused with the basis aspect is it a member of transform...e.g. transform.basis...yeah I'm lost with basis.

Here's some detailed info: http://codetuto.com/2017/02/direction-vectors-godot-engine/

Transform is an object containing a matrix 32 for the direction(rotation) and a Vector3 for location (x,y,z) http://docs.godotengine.org/en/stable/classes/class_transform.html#class-transform

Transform has two public attributes.

origin

The origin is a simple Vector3 with the x/y/z coordinates. In global context (get_global_transform().origin) these are the absolute coordinates in 3D context. Simple.

If the object is a child object of another spatial, then (get_transform) these are the local coordinates inside the parent objects coordinate space. For example: You have an airplane (parent) and on the "back" of the airplane there's a rudder (as child). The airplane is (initially) facing to negative z. This rudder is i.e. 3m back from the center (0,0,0) of the plane. Then it's local origin is Vector3(0,3,0).

When the plane moves 10m meters forward then the planes origin (global and local) changes to (0,-10,0). The rudders local origin stays the same (get_transform().origin) but the global origin (get_global_transform().origin) changes accordingly to (0,-7,0)

When instead, the plane turns instead 90° around the y-axis to the left. The global origin of the rudder changes to (3,0,0). The local origin stays at (0,3,0).

basis

This is a Matrix32. It contains 3 x Vector3. One for each axis. These vectors tell where the original x/y/z Vectors point after the rotation. So if the object points forward (I mean: no rotation). Then the vectors are like this: basis.x = Vector3(1,0,0) basis.y = Vector3(0,1,0) basis.z = Vector3(0,0,1)

You rotate the object 90° to the left (around y-axis) then the values are like this basis.x = Vector3(0,0,-1) basis.y = Vector3(0,1,0) basis.z = Vector3(1,0,0)

For distinction of global/local when can look at a tank as example. The tank has a rotating turret as a child object.

Now the tank itself is following a course and is rotated. The turret is also rotated relative to the parent. (Shows to a different direction than the tank.)

Now you could get the local rotation vector for z with: get_transform().basis.z * -1 (-1 is only multiplied here to get the "forward" direction) Might be useful if you want to move some other child object relative to the turret in code. (The value above shows the relative positional offset 1m to the front of the (locally) rotated child.)

But much more interesting in this case it is to get the global direction of the turret. get_global_transform().basis.z * -1 will give you the absolute forward direction vector of the turret. I.e. useful for raycasting and/or moving bullets/rockets.

Angles vs. vectors: You have to get used to the aspect that the direction is defined by vectors and not by angles. There are reasons for this (gimbal lock) which I won't (and probably can't) detail here. You should just avoid angles as much as possible when calculation with rotation in 3D as you often won't get usable results. Generally it should be ok to use set_rotation() on spatials but don't expect to get the same angles back on get_rotation() ;-)

Hope I didn't do any errors in the samples.

I tried your recommendations, aside from impulse...and I still just end up with global transform. I'm really just interested in getting local transforms...the simplest way possible, once I figure that out I can go from there.

`func _ready(): set_fixed_process(true)

func _fixed_process(delta): var speed #var walk_dir = Vector3(get_linear_velocity())

if Input.is_key_pressed(KEY_W):
	speed = 1
	#walk_dir[2] = 1.0
else:
	speed = 0
	#walk_dir[2] = 0.0
	
translate(get_transform().basis.z.normalized() * delta * speed)
#set_linear_velocity(walk_dir)`

somehow our comments were posted at odd times... ok, I fully understand what global coords and local coords are, that was never an issue :) I understand what a vector is and the given direction of length is -1 - +1 that is not an issue....I am just asking how to translate something based on it's local coordinates...that is all..I have tried repeatedly...I find it off there is not simply a translate_local() function :) this is not my first time scripting, I have been doing this since 2001/2002 or so....but I have always had an engine that was aimed at 3d games and this is clearly not the case here...it feels like 3D was built on top of the 2D....and the documentation...I get so frustrated with the horrible documentation. If I can't tackle this soon I am going to pull my hair out.

ok, this insanely simple code worked.

` var speed

if Input.is_key_pressed(KEY_W):
	speed = 1.0
else:
	speed = 0

translate(Vector3(0,0,delta * speed))`

translate seems to use the local coordinates... and this also seems to be the simplest way to do it...but since I just picked up the engine last week or so...I am not the most reliable source.

Perhaps it helps if you describe more exactly what you're aiming to do with local translate.

A spatial has two translate methods: global_translate() translate()


The latter IS a local translate. (or translate_local)

I agree that the documentation is sparse for 3D topics. I wish there was some kind of possibility to post comments and example code for classes and their methods directly on the documentation site. Like the "User contributed notes" on the php site ( php.net/manual/en/function.preg-match.php )

You have to pull what is required from several scattered postings and youtube videos.

But generally, I'd say godot isn't bad for 3D games.

I have just been frustrated, I like godot...I think my frustration has just been compounded by the poor documentation.(lists of functions without explanations or giving examples)..as stated I did get translate() to work....now I am trying to find mouse movement, likely a vector2...but again the docs are just a nightmare for me :) ...I did update my first post in case "lukoz" returns for an answer. A general rule in 3D development is almost all of your translations are in local space, unless you inherit motion from a platform or something....like an escalator or just some mario style lateral moving platform.

6 years later