- Home /
Weird Camera snap
I made two scripts, a movement script for the player and a rotation script for the camera that is following him. I have it so that the movements script can use the W, A, S, and D keys to move around and the mouse to rotate the player. My Camera script uses the mouse to rotate around the player. I made it so that whenever the player is standing still the camera can rotate around the player, however, when the camera moves the camera can no longer rotate and it switches to the player rotation while the camera stays locked on the player. My problem starts when I stop moving for the second time the camera snaps to the front of the player and it's really annoying. I have no idea what's causing the problem, I thought it may have been the camera still rotating and then when i stop it shows where the camera rotated to however that's not the case because in the scene the camera is following the player the whole time.
Player Movement:
//Simple movement script
transform.Translate (speed * Input.GetAxis ( "Horizontal" ) * Time.deltaTime, 0f, speed * Input.GetAxis ( "Vertical" ) * Time.deltaTime);
//Rotation Script
if (canRotate == true)
{
transform.localEulerAngles += new Vector3(0, Input.GetAxis("Mouse X") * speed, 0);
}
if (lastPosition != transform.position)
{
canRotate = true;
}
else
{
canRotate = false;
}
lastPosition = transform.position;
Camera Movement:
if (canRotate == true)
{
if (target)
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
}
Answer by lgarczyn · Jan 24, 2016 at 10:14 PM
Your problem might be this
transform.localEulerAngles += new Vector3(0, Input.GetAxis("Mouse X") * speed, 0);
Simply put, you should never read any rotation from your transform, only write. Quaternions are weird, and EulerAngles are worse.
So modify a Vector2 storing your x and y rotation, and then create a new Quaternion from that.
Also, you didn't put any camera reset code, so I don't know if you actually set the camera Quaternion back to identity or not, if canRotate happens to be false.
Alright, I changed the euler to this:
if (canRotate == true)
{
float rotY = Input.GetAxis("$$anonymous$$ouse X") * mouseSensitivity;
transform.Rotate(0, rotY, 0);
}
Which works fine, however, it still does the snap thing. Once my character stops moving and switches to the camera rotation the camera snaps in the opposite direction. It probably sounds confusing and I wish I could add a gif of what's happening. Also, how would I go about setting the camera reset code?
Your answer
Follow this Question
Related Questions
Third Person Camera Help? 0 Answers
Distribute terrain in zones 3 Answers
How to keep the player on the left side o the screen? 2 Answers
making a camera stay at the right distance (c#) 3 Answers
Third-person camera snaps to 270 degrees on activation 1 Answer