I'm working on making a third person, 3D character controller and I want my character to move left/right/up/down relative to where my camera is facing. Rather than always moving in the same direction when I press "W". Even if I'm looking to my right with my camera. I'm not sure how to math this out and am now stuck lol. Any tips, or information would be super appreciated. I am using C# but I don't think that will be super relevant to the issue.
#Moving player relaitive to camera
15 messages · Page 1 of 1 (latest)
you basically need to take your camera's orientation and apply the relevant part of it to your input.
depending on how complex your camera movement is, this might be as simple as grabbing camera.global_rotation.y and rotating your intended movement vector by that.
if your setup is weirder (eg. walking on ceilings, walls or weird angles), you might instead want to approach the issue by defining a flat plane in which the movement (roughly) happens, grabbing the camera's forward vector and projecting it onto that plane, and then rotating that vector based on the input info to define the movement direction. you then probably normalize that vector because it might be quite squished and multiply by the magnitude of the input to get your real movement intent vector. this is all only needed if you actually intend to have such weird movement. the basic version is enough for 90+% of games.
here's code from my current project that does the basic version for joystick input
if camera:
const deadzone := 0.2
var pads := Input.get_connected_joypads()
if pads.size() == 0:
return
for pad in pads:
var l_r_axis := Input.get_joy_axis(pad,JOY_AXIS_LEFT_X)
var d_u_axis := Input.get_joy_axis(pad,JOY_AXIS_LEFT_Y)
var net_direction := Vector3(l_r_axis, 0, d_u_axis)
if net_direction.abs().x < deadzone && net_direction.abs().z < deadzone:
return
net_direction = net_direction.rotated(Vector3.UP, camera.global_rotation.y)
omg that makes so much sense idk why i was trying to overcomplicate that in my head lol
i'll try that tommorow and let you know how it goes, thanks so much for the information!
so im definetly still confused
I'm gonna try to learn some more Godot basics about 3d and pray that helps, while I wait. cause I don't even know the right questions to ask lol.
I don't know godot's c# bindings, sadly, so can't advise much on the code you've got there, but it looks like you're applying the ENTIRE rotation you passed in instead of only the rotation around the vertical axis.
in gdscript, the needed call takes two params: an axis to rotate around, and the amount to rotate around it. There's probably an equivalent call in C# somewhere.
yeahhhhh, I am stuck stuck.
I've come up with a new idea to get around this issue but I can't figure out how to get a transform3ds global position in C#