+2 votes

I've been trying to get my character mesh to rotated based on the direction of my character. Like if I move up, it will face up, if I move left it will face left, If I go right and down it will face southeast.

How do I do this

P.S. It's a spatial project.

in Engine by (272 points)

Is it a top-down view or what? Otherwise that doesn't make much sense. Anyway, all you need to do is define a loop in fixed process that rotates the character in each frame, then when you detect the input the rotation value will change depending on the key.

If you could provide at least a screen capture of your scene it would be great.

1 Answer

0 votes

There are different approaches you can take depending on what kind of game you're making. A really basic example that assumes the character is moving in the x-z plane and points it in the direction of travel:

var vel = Vector3()

func _fixed_process(delta):
    var tf = get_global_transform()
    if Vector2(vel.x, vel.z).length_squared() > 0.001:
        tf.basis = Matrix3(Vector3(0, 1, 0), atan2(-vel.x, vel.z))
    tf.origin += vel * delta
    set_global_transform(tf)

You might have to change the signs in the atan2 command if your character points the wrong way.

by (1,556 points)
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.