#FPS Camera Rotating the wrong way
48 messages · Page 1 of 1 (latest)
public override void _UnhandledInput(InputEvent @event)
{
if(Input.IsKeyPressed(Key.Escape))
{
Input.MouseMode = Input.MouseModeEnum.Visible;
}
if(@event is InputEventMouseMotion)
{
InputEventMouseMotion a = (InputEventMouseMotion) @event;
//this.RotateY(a.Relative.X * camSensitivity * -1 * Convert.ToSingle(GetProcessDeltaTime()));
GD.Print(this.Rotation.X);
this.RotateX(a.Relative.Y * camSensitivity * -1 * Convert.ToSingle(GetProcessDeltaTime()));
}
}
what is the issue here?
One second let me quickly get a video of whats happening
if the this.rotatey function was uncommented this would happen
im not sure why its rotating like that, i just want it to rotate like a standard FPS camera would but 🤷♀️
looks a bit like a gimbal lock thing to me.
try changing the order of rotations or maybe use a pivot node parent to keep the rotations separate
do you mind showing a example on how to do that?
okay, lets start with node hierarchy, can you put up a screenshot how it looks like for the player scene?
this?
first, lets make the camera a child of the player, so you make sure it moves where the player moves too
(I guess the characterbody is your player)
At the moment im just trying to make the camera movement seperate from the player controller
unless thats where im going wrong, i do have a character in the level but its literally just using the example code that godot has when you generate a script that inherits characterbody3d
ah, okay. then just create a new node named 'pivot' and put the camera under this new node
Like this?
yes. I think you need a Node3D, you can change it via rightclick -> change type
There now its a Node3D
now, coming back to the camera script, you can change
this.RotateX(a.Relative.Y * camSensitivity * -1 * Convert.ToSingle(GetProcessDeltaTime()));
to
GetParent().RotateX(a.Relative.Y * camSensitivity * -1 * Convert.ToSingle(GetProcessDeltaTime()));
this might already work like this
the cameras transform might not be 0
can you open the camera node in the inspector and click this?
I'm checking a tutorial on this
https://kidscancode.org/godot_recipes/3.x/g101/3d/101_3d_07/index.html
they actually use a pivot too, but also clamp it afterwards
How do i clamp the pivot?
something like this:
Node3D pivot = GetParent<Node3D>();
float xClamped = Mathf.Clamp(pivot.Rotation.X, -1.2f, 1.2f);
pivot.Rotation = new Vector3(xClamped, pivot.Rotation.Y, pivot.Rotation.Z);
not sure if theres a simpler way
yea still doing it :/
do you have any rotations on the pivot or camera nodes?
Nope
oh, maybe you need to do the y-rotation on the pivot and x-rotation on the camera
wait give me a couple minutes
You could do something like this inside your character controller:
private Vector3 viewAngles = Vector3.Zero;
private Vector2 mouseDelta = Vector2.Zero;
...
// IMPORTANT: do this in _Process, NOT THE INPUT HANDLING METHODS
viewAngles.Y -= mouseDelta.X;
viewAngles.X -= mouseDelta.Y;
// clamp viewAngles here and whatnot
...
camera.Rotation = viewAngles;
mouseDelta = Vector2.Zero;```You would use `e.Relative` to determine `mouseDelta`, and of course obtain a reference to your camera node before all that
Here's how I do my mouseDelta:
if ( @event is InputEventMouseMotion && Input.MouseMode == Input.MouseModeEnum.Captured )
{
var mouseMotion = @event as InputEventMouseMotion;
mMousePositionDelta += mouseMotion.Relative;
}```
The `Input.MouseMode` check is just to see if the player's using a menu, as to avoid rotating there
So TL;DR viewAngles should be a member of your character controller, and over time it just changes with mouse motion
And it gets applied to the camera
Oh yeah mouseDelta is also a member
There we go, updated pseudocode