Wii Tanks movement in Godot. having trouble with rotation

MagictwicksMagictwicks Posts: 3Member

So I just started learning Godot and for my first little prototype project I'm trying to make a recreation of Wii Tanks. I've been trying to get the movement to match but I've had no luck. Using an xbox 360 controller my goal is to have the character slowly rotate towards the direction my joystick is pointing and go forward or backward on the z axis depending on which is the closest angle to rotate to.

Comments

  • cyberealitycybereality Posts: 1,086Moderator

    Rotating based on analog stick movement should be simple. Just read the x-axis of the stick and add that to the local rotation of your tank.

    var horz = Input.get_action_strength("look_right") - Input.get_action_strength("look_left")
    rotate_y(-horz * stick_speed * delta)
    

    In this case I have set up an action map for a joy axis in the project settings.

    Stick speed is an export var that controls how fast the object spins.

    Then for the forward movement, you need to transform the z-axis into a local coordinate frame.

    Near the top of the script (outside any function)

    export var move_speed = 1.0
    export var stick_speed = 1.0
    var forward_vec = Vector3(0.0, 0.0, -1.0)
    var up_vec = Vector3(0.0, 1.0, 0.0)
    

    Then in _physics_process:

    var basis = get_transform().basis
    var forward = basis.xform(forward_vec)
    

    Now you can use forward to move your tank, like with "move_and_slide".

    if Input.is_action_pressed("forward"):
            move_and_slide(forward * move_speed, up_vec)
    

    You'll have to customize move and stick speed to what you want. Hope that helps.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file