0 votes

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

in Engine by (52 points)

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.

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.
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.