- Home /
Issue with Camera Tilt & Movement at the same time
Hello and thanks for reading my thread! I am currently working on the camera system of my game (third person). Anyway, I used this code to adjust my camera position when its being tilt or moved:
public void CameraSync()
{
if (!_Errors)
{
// Calculate the camera rotation and position based on the
// Mouse X and Y movement
Quaternion rotation = Quaternion.Euler(_y, _x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + CameraPos.position;
_myTransform.position = position;
_myTransform.rotation = rotation;
}
}
I call this function once on start, once per frame. The player moves with another called CharacterMovements. This way the camera "acts" like parented to the player. Im rotating the camera when right mouse button is being held down:
if (Input.GetButton("Mouse RightClick"))
{
if (_CameraMode != "First Person")
{
Screen.showCursor = false;
Screen.lockCursor = true;
_x += Input.GetAxis("MouseX") * xSpeed * 0.02f;
_y -= Input.GetAxis("MouseY") * ySpeed * 0.02f;
}
All of the above works. I can rotate the camera any way I want with my mouse, the camera is following the invisible CameraPosition gameobject that is located a little bit above the players head.
The thing I'm trying to change is that when my player turns right by an angle _x around the Y axis, I want the camera to do the rotation at the same time (So the camera looks 'locked'). I was using this code:
if (Input.GetButton("Turn"))
{
_x = _player.transform.rotation.eulerAngles.y;
}
Where _player is a cached version of the well player ^^ and transform.rotation refers to the camera rotation. This worked out too. Now my camera is always behind the player. Problems start when I try to rotate my camera angle with RightMouse .. as expected. On start of the game for example both rotations are 0°. I turn the player for 90° to the right, the camera follows. But when I tilt the camera for 90° example and then move the player, it snaps right behind the players back again, instead to lock in the new position. Thats because my code above draws an equal sign between camera and player rotation.
I just don't know how to fix it.. I tried to add player and camera rotations up but that didn't work either, the camera started to rotate like mad around the player.
Hope I explained everything good enough! If there is any other questions just ask.
Thanks in Regards ~Ilhan
Answer by Rod-Green · Nov 17, 2011 at 08:54 PM
Why dont you just make the camera a child of the head? So any movement's/rotations of the head are applied (via hierarcy) to the camera.
If you want to tilt the camera outside of the head movement/rotation then all you need to do is apply the rotation to the camera (while child of the head) and it'll offset the rotation by the values you set.
If that doesn't work then you might need to just break the different transform types into unique gameObjects that are set to the parent of the camera transform.
i.e.
translationPiv
->rotationYPiv
->lookatPiv
->Camera
that way any values you apply in each stage of the hierarchy don't have an affect on the next.
Also as an FYI in any of these situations you probably should be using localRotation not rotation.
Answer by Ilhan · Nov 17, 2011 at 09:47 PM
Well I tried to make the camera a child of the parent earlier too, but for some reason ( I think its something with CameraSync() ) my camera doesen't get rotated even as child of the player, or the sync function overhauls the cameras intend for rotation as child (because the update is called once per frame).. or something like that. Thanks for pushing me in the right direction though I have to agree that I almost forgot about parenting :)
Answer by Ilhan · Nov 18, 2011 at 12:27 PM
Sorry, didnt see the "Add New Comment" button below xD (new to the forums).
Anyway, I figured it out this was the answer:
I locked the rotation so it only happens if the right mouse button is being held down, rather then update its positio/rotation every frame and parented the camera to the palyer. Works like a charm, thanks for the help.
(Hope I posted this right as "Answer" this time)
Thanks a lot for the help again.
Your answer
Follow this Question
Related Questions
Orbit around an object 0 Answers
How to lock camera angle on key press? 1 Answer
Lock look rotation first person. 0 Answers
Set Max Rotation On Weapon Sway 0 Answers