Coming From Unity To Flax? Watch This Video On Coding Basic 3D Movement

Here’s an example :slight_smile: The video below has the full code.

using FlaxEngine;


namespace Game;

/// <summary>
/// Movement Script.
/// </summary>
public class Movement : Script
{
    /// <inheritdoc/>
    private bool MoveForward => Input.GetKey(KeyboardKeys.W);
    private bool MoveLeft => Input.GetKey(KeyboardKeys.A);
    private bool RotateRight => Input.GetKey(KeyboardKeys.E);
    private bool RotateLeft => Input.GetKey(KeyboardKeys.Q);

    private float _speed = 10f;
  

    /// <inheritdoc/>
    public override void OnUpdate()
    {
        
        if (MoveForward)
        {
            Actor.AddMovement(Actor.LocalTransform.Forward * Time.DeltaTime * _speed); //moves in forward facing direction

//  Actor.AddMovement(Actor.Direction * Time.DeltaTime * _speed);   //also moves Actor in forward facing //direction

        }
        if (RotateRight)
        {
            Rotating(_speed/300f);  //rotates very fast otherwise
        }
        if (RotateLeft)
        {
            Rotating(-_speed/300f);
        }
        if (MoveLeft)
        {
           Actor.AddMovement(Actor.LocalTransform.Left*Time.DeltaTime*_speed); //moves in the direction of the Actor's left, not world left
        }
        // Here you can add code that needs to be called every frame
    }

    private void Rotating(float rotSpeed)
    {
        Actor.Orientation *= Quaternion.RotationY(rotSpeed)*Time.DeltaTime; //rotates the Actor along the specified axis. In this case the Y axis. This will change it's forward facing direction
       
    }
  
}
1 Like