I've been fighting with getting a vector directly in front of the player, trying so many different ways, but my vector math(and general coding skills) are not so great...

I am basically trying to get:

var vec = global_transform.xform(node.global_transform.basis[1] * Vecotr3(0 , 0, offset))

so that when ever this entity rotates the object maintains it's distance, but rotates with the entity. I also tried getting it from the orientation as well...seems like a must, but I can't get anything working. any help would be appreciated

ok, I have some working code, but I don't like this implementation...seems that there is a better way...I put this on a mesh instance to test it and it works, but if there is a better way(less hacky) please show me ..

extends MeshInstance

onready var player = get_tree().get_root().get_node("world/player")

func _process(delta):
	
	var dist = 2
	look_at(player.global_transform.origin,Vector3(0,1,0))
	global_transform.origin = player.global_transform.origin
	rotation = player.rotation
	translate_object_local(Vector3(0, 0,dist))

You should be able to do something like this:

var forward = Vector3(0.0, 2.0, 0.0)

func get_transformed():
	var basis = player.get_transform().basis
	return basis.xform(forward)

thanks, I will check it out after my coffee :)

Edit: No, sorry, it does pretty much the same thing that I was able to do before..kinda moves an object left and right based on the rotation

it should be something like...


"object.global position(global vector) + object.global orientation[axis] * distance"# dummy code

I just am very un familiar with the api and getting through the documentation is a bit awkward... I'm not a total idiot, but I'm no genius either ;)

Can you better explain what you are trying to accomplish? I'm not sure I totally understand now.

I'm trying to lock an object(or objects) to another objects location, with an offset...like the moon to the earth.

the only difference is when the "parent" object rotates the "moon" object should rotate with it, while keeping the offset distance.

I know this might sound rude, but I mean it sincerely, should I draw you a picture? Edit: notice in the image the pink/purple object is rotating and the yellow object maintains it's offset distance...as if it is parented, but the rotation of the yellow object is not relevant...it's just how I made the image.

Maybe try this:

var offset = Vector3(1.0, 0.0, 0.0)

func get_transformed():
    var basis = player.get_transform().basis
    return basis.xform(offset) + player.translation

func _process(delta):
    second_object.translation = get_transformed()

With slight modification, yes it works...I am calling many functions from another spatial node with some helper functions in it's script, so I need to take into account both objects..so I've done it this way(see below).

func get_transformed(node1,node2,offset):# this is in the "helper" node
	var basis = node2.get_transform().basis
	var loc =  basis.xform(offset) + node2.translation
	node1.translation = loc
        
		
		
func _process(delta):
	helper_functions.get_transformed(self,player,Vector3(0,0,4))# and here I can call it.

I put many little functions in this in there, It makes my code much easier to comprehend. I thank you for your time, and help. Much appreciated.

3 years later