Godot Version
4.2.2
Question
I am getting back to programming after a long break, and I am following a tutorial for an FPS controller (“Make An FPS in Godot 4” by StayAtHomeDev on Youtube). The tutorial is using GDScript and I am trying to translate it to C#.
I have hit a roadblock in the camera control section of the code that ensures that the player’s directional movement reacts to the camera’s position and movement. The player’s model is currently locked from rotating along the X and Z axes, with the camera only being allowed to rotate along the X and Y.
However, I am having trouble in getting the player’s movement to match the camera’s direction along the Y axis - that is, the player’s “forward” should be towards the camera but it instead remains locked to moving across the standard X and Z axes.
My UpdateCamera function is as follows; it is called during _PhysicsProcess() and takes the double Delta as an variable:
public void UpdateCamera(double Delta)
{
mouseRotation.X += tiltInput * (float)Delta;
mouseRotation.X = Mathf.Clamp(mouseRotation.X, tiltLowerLimit, tiltUpperLimit);
mouseRotation.Y += rotationInput * (float)Delta;
playerRotation = new Vector3(0.0f,mouseRotation.Y,0.0f);
cameraRotation = new Vector3(mouseRotation.X,0.0f,0.0f);
// cameraController
Transform3D newTransform1 = cameraController.Transform;
newTransform = cameraController.Transform;
newTransform1.Basis = Basis.FromEuler(cameraRotation);
cameraController.Transform = newTransform1;
// GlobalTransform
Transform3D newTransform2 = GlobalTransform;
newTransform2.Basis = Basis.FromEuler(playerRotation);
GlobalTransform = newTransform2;
cameraController.Rotation = new Vector3(
cameraController.Rotation.X,
cameraController.Rotation.Y,
0);
rotationInput = 0;
tiltInput = 0;
}
In the video, the section in charge of handling the “correct” direction is the GlobalTransform section, so I have tried to research that. Unfortunately, I have only been able to find resources for GDScript about it.
If it helps, here are the relevant variables used by the UpdateCamera function, located outside of it:
[Export]
public float tiltLowerLimit {get;set;} = (float)Mathf.DegToRad(-90.0);
[Export]
public float tiltUpperLimit {get;set;} = (float)Mathf.DegToRad(90.0);
[Export]
public Node3D cameraController {get;set;}
public Vector3 mouseRotation;
public float rotationInput;
public float tiltInput;
public Vector3 playerRotation;
public Vector3 cameraRotation;
All help in solving this issue is greatly appreciated.