First-person movement issues in C# - Godot 3.0.2 Stable Mono

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ben Beshara

Hi - I’m currently trying to write a first-person movement script in C# based off an earlier script I wrote in GDScript. The original script worked fine, but I’m having trouble getting the movement to work properly under C# and I’m not sure why.

The basic problem is, that when the player looks left or right, the movement becomes inverted, so pressing ‘forward’ pushes the player backward, etc.

Code snippets follow:

This is an extract of the code I’m using to move the player:

public override void _PhysicsProcess(float delta){
    Vector3 basis = new Vector3(0, 0.9f, 0);
    Basis bodyTransform = playerBody.GlobalTransform.basis;

    if(Input.IsKeyPressed((int)KeyList.W)){
        playerBody.ApplyImpulse(
            basis,
            bodyTransform.z * -speed
        );
    }
}

And this is the code I’m using to rotate the player:

public override void _Input(InputEvent @event){
    if(@event is InputEventMouseMotion){
        InputEventMouseMotion mouseMotion = (InputEventMouseMotion)@event;
        cameraNode.RotateX(-mouseMotion.Relative.y * GetPhysicsProcessDeltaTime());
        playerBody.RotateY(-mouseMotion.Relative.x * GetPhysicsProcessDeltaTime());
    }
}

Any help would be very much appreciated!

TIA

Solved it! the transform structure wasn’t what i expected in C#, managed to use new Vector3(bodyTransform.x.z, 0, bodyTransform.z.z) as my movement vector.

Ben Beshara | 2018-04-10 08:02