The question is answered, right answer was accepted.
Making a First Person Camera using an external asset, how to make the inputs match the Camera Rotation?
I am new to Unity, so forgive me if the answer seems obvious. I am making a First Person Shooter, or at least trying to. I am using a modified version of the "FPSWalkerEnhanced" script that I re-wrote, as well as the famous Camera Controller by OOTII. However, I have run into a problem that after spending the last week trying to figure out, have decided to turn to the nice people of the internet for help. Whenever I turn the Camera in the game, the inputs do not match the camera's direction. For example, if I turn the camera to the right, then push forward, I go left. If I turn the Camera to the left, then push forward, I go right. After some messing around, I think it has something to do with the Player GameObject not rotating with the Camera. But, I cannot figure out how to make just the y-rotation of the Player match the y-rotation of the Camera Rig, and I don't even know for sure if this will even fix it though. When I asked the author of the Camera Controller, he referred me to his example script included with the asset, and told me that his example script takes the camera’s view to determine if it should be relative. But, after reading the code, I don't understand what he is trying to do. Any help would be appreciated.
Answer by Infenix · Apr 06, 2020 at 05:47 PM
I think what you're looking for is the Transform.TransformDirection function. Usually, when making a camera controller, i read user inputs and store them all in a simple vector3 called movemnt. Then, i use it with the functions which gives me :
Vector3 movement = //User Inputs, depends on the game
transform.position += transform.TransformDirection(movement) * movementSpeed;
You just have to readapt it so your camera fits you player position (maybe need an offset ?)
If that's not what you're asking don't hesitate to explain me better what you try to achieve.
@Infenix Thank you for your answer, but transform.TransformDirection(movement) is already in my script. I think the problem lies with the GameObject that represents the Player is not rotating to match the Camera. In the Camera Controller asset that I am using, the one made by ootii, the $$anonymous$$ain Camera is not childed under the Player, but is ins$$anonymous$$d childed under another GameObject titled "Camera_Rig". I cannot figure out how to get the Player's y-rotation to match the Camera_Rig's y-rotation (As the "Camera_Rig is the one rotating, no the $$anonymous$$ain Camera itself). I don't know if I should be using Euler Angles or not, I have tried assigning the y-rotation directly: _playerTransform.rotation.y = _cameraRig.rotation.y;
. But, that gives me an error that says something about it needing to ba a variable. I then tried _playerTransform.rotation = new Vector3(_playerTransform.rotation.x, _cameraRig.rotation.y, _playerTransform.rotation.z);
. Another error states that it cannot convert a Vector3 into a Quaternion. When I tried using a Quaternion, the Player just starting flipping all over the place. I am at a complete loss.
Oh ok, didn't understand it. Then, yes you want to pass the rotation to the other gameobject. If you don't know how quaternions work, it effectively may lose you fast. Setting the attributes one by one is impossible so you have to do it the way you tried. However, quaternions are 4D vectors so it is impossible to create one from a vector3 and that it matches your expectations. Then, if this time i understood, you have to use
transform.rotation = Quaternion.Euler(_playerTransform.rotation.x, _cameraRig.rotation.y, _playerTransform.rotation.z);
@Infenix I appreciate your patience in help me. After implementing your suggested code, the Player's GameObject experienced some y-rotation, but for some reason did not match the rotation of the Camera Rig GameObject. For example: I rotated the Camera Rig to around 170 degrees, the Player only rotated to around 0.19 degrees.