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.
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 Main Camera is not childed under the Player, but is instead 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 Main 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.