0 votes

Hello. I'm a bit stuck with implementing character movement for my little project.

I want pretty much the same controls as in Overcooked game. When a player presses UP key, the game character moves up, when a player presses DOWN key – the character moves down the screen. And that is very easy to implement with global translation.

I can't handle the character rotation though. For example at some particular moment character looks down. At this moment player decides to send the character UP. At the very beginning of the movement, I want the character to rotate to direction of movement – UP.

And I want it to be a smooth transition.

I did some monstrous code which looks pretty much like this:

extends Spatial
# Declare member variables here. Examples:
# var a = 2
# var b = "text"

var MOVE_SPEED = 6

# Called when the node enters the scene tree for the first time.
func _process(delta):
    var dx = 0
    var dy = 0

    if Input.is_action_pressed("ui_left"):
        dx += 1
    if Input.is_action_pressed("ui_right"):
        dx -= 1
    if Input.is_action_pressed("ui_up"):
        dy += 1
    if Input.is_action_pressed("ui_down"):
        dy -= 1

    var direction = Vector3(dx, 0, dy) * delta
    translate(direction * MOVE_SPEED)

    # |--------|
    # |ROTATION|
    # |--------|

    if dx != 0 or dy != 0:
        var targetRot = 0
        var currentRot = fposmod($Rotation.rotation.y, 2 * PI)

        if currentRot > PI:
            currentRot = - (2 * PI - currentRot)

        if dx == 1:
            targetRot = 0
        if dx == -1:
            targetRot = PI
        if dy == 1:
            targetRot = -PI/2
        if dy == -1:
            targetRot = PI/2

        print(currentRot, ' -> ', targetRot)

        if abs(targetRot - currentRot) > PI / 360:
            var delRot = 1 if currentRot < targetRot else -1
            var minRot = min(currentRot, targetRot)
            var maxRot = max(currentRot, targetRot)
            $Rotation.rotation.y = clamp(
                currentRot + delta * delRot * ROTATION_SPEED,

I believe – there's a better and less buggy solution than mine.

asked May 27 in Engine by shirokoff (12 points)

If all else fails you could do it with a simple animation. Last resort, It would be better to do it in code IMHO.

I'm not really sure how to apply animation in this case. I need an angle to rotate. But I'm not sure how to efficiently calculate it.


here's a link to a similar question, maybe it can help. :) I think you can get the mouse position with getglobalmouse_position()

I guess I've stated the problem with a lack of details. I don't want mouse to be used.

I think this video might be useful

oh, sorry, I got confused between several questions. oops. :) I'm a bit new to Godot, so I'm not positive how to do this. Hopefully someone else will know. :)

Thank you for the help.

I finally figured out how to solve it. I remembered a CS problem of building a convex hull for a shape. And there you would use the fact that vector cross product will be negative or positive depending on what side of vector A lais point B.

var cross = faceDirection.cross(direction)
var s = 1 if cross.y > 0 else -1
var angle = faceDirection.angle_to(direction) * delta * TURNING_SPEED * s
rotate(Vector3(0, 1, 0), angle)
faceDirection = faceDirection.rotated(Vector3(0,1,0), angle)

Please log in or register to answer this question.

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.