+1 vote

Hi guys, I wanna make my character face the direction to where he is heading.
Here's the script:

public void Move()

    velocity = new Vector3();
    if (Input.IsActionPressed("right"))
        velocity.x += 1;
    if (Input.IsActionPressed("left"))
        velocity.x -= 1;
    if (Input.IsActionPressed("down"))
        velocity.z += 1;
    if (Input.IsActionPressed("up"))
        velocity.z -= 1;
    velocity = velocity.Normalized() * speed;

public override void _PhysicsProcess(float delta)
in Engine by (188 points)

1 Answer

+1 vote
Best answer

Make the character MeshInstance(s) children of a spatial and use look_at()

character_spatial.look_at(transform.origin + velocity, Vector3.UP)
by (3,223 points)
selected by

Unfortunately, it doesn't work, probably because it is a Sprite3D.
In 2D there is this simple method:

sprite.rotation = velocity.Angle();

In 3D it's another story since it uses a Vector3 as rotation, is there any ways I can mimic the same thing?

2D rotation is a float, 3D rotation uses Basis which can be set by a basis, quaternion or Vector3. There isn't a 3D method similar to angle(). look_at() is the easiest method to use here otherwise you need to get a target rotation and rotate to it. The only other workaround I can think of (but can't guarantee if it'll work) is cheat using Vector2(Vector3.x, Vector3.z).angle() and set rotation_degrees.y to the returned float.

In this case your Vector3 will be the velocity variable.

Looks like setting the rotation.z to new Vector2(veclocity.x, velocity.z).angle() does the trick. Thanks!

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 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 webmaster@godotengine.org with your username.