How to copy rotation and translation of a parent plus some offset?

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

Hi! I have a character and a vehicle. When I parent a character to a vehicle I want a character to copy the facing direction of a vehicle and the position.


Green arrow represents a vehicle and a blue one is a character when entering a vehicle from the side, then inside a vehicle. Character should face the direction of a vehicle. Then when exiting a vehicle a character should too copy the direction with and offset respresented by red dotted line.

My wrong code is something like this:

# Entering a vehilce
global_transform.origin = vehicle.transform.origin + Vector3(0.5, 1.75, 0)
shape.rotation.y = vehicle.transform.basis.get_euler().y

# Exiting a vehilce
global_transform.origin = vehicle.transform.origin + Vector3(2, 1, 0)

I’m very bad at this, sorry.

:bust_in_silhouette: Reply From: Zylann

Like this?

var vtrans = vehicle.transform
var ptrans = Transform()

# Copy orientation of the vehicle (I'm assuming you didnt scale the vehicle)
ptrans.basis = vtrans.basis

# Sets position to be on one side of the vehicle (along its X axis)
var distance_from_vehicle = 1.0 # Tweak this
ptrans.origin = vtrans.origin + vtrans.basis.x * distance_from_vehicle

global_transform.origin = ptrans

That helped with positioning. Thanks! But how do you adjust the rotation? So the direction of a character and a vehicle stayed the same whatever is the rotation of a vehilce in the world.

Edit: Ok! I got it! I copy the rotation of the vehicle node but it doesn’t change in the world. Instead I should copy the rotation of the vehicle’s visual body, which changes rotation.

wowzzers | 2019-10-03 18:44