This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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.

in Engine by (81 points)

1 Answer

+1 vote
Best answer

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
by (29,510 points)
selected by

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.