- Home /
3rd person camera reset bug
I'm working on writing up a camera control script for a game I am doing and have run into an odd issue. The camera itself controls fine, bound to the keyboard and a controller. However, upon hitting the reset button to center the camera it has some odd features. Normally it zooms out very slightly and 'pops' back in, which is somewhat jarring but not game breaking. The worst problem is when resetting from a high position, such as looking down at the player, in which it jumps much more noticeably. Also, upon migrating the script to C# in order to have more functionality it jumps terribly when resetting from below the terrain as well. Currently there is no collision with terrain or objects, that is my next step.
If anyone can see what is causing the jittery movements and jumping I would be most indebted. In the meantime I'm going to work on the collision, since I've hit a dead end on this particular issue.
Here is my code:
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour {
public Transform target;
public float distance = 7.0F;
private string state = "mid";
public float height = 3.0F;
public float heightDamping = 2.0F;
public float rotationDamping = 3.0F;
private float zoomtime = 0.0F;
private float zoomdelay = 1.0F;
private float camtime = 0.0F;
private float camdelay = 0.5F;
public float xSpeed = 180.0F;
public float ySpeed = 80.0F;
public float yMinLimit = -20.0F;
public float yMaxLimit = 80.0F;
private float x = 0.0F;
private float y = 0.0F;
private float wantedRotationAngle = 0.0F;
private float wantedHeight = 0.0F;
void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void Update () {
if (zoomtime <= zoomdelay)
{
zoomtime += Time.deltaTime;
}
if ((Input.GetAxisRaw("DPad_Vertical") == 1 || Input.GetAxisRaw("Mouse ScrollWheel") > 0)&& zoomtime >= zoomdelay)
{
if (state == "far")
{
state = "mid";
distance = 7.0F;
}
else if (state == "mid")
{
state = "close";
distance = 4.0F;
}
zoomtime = 0.0F;
}
else if((Input.GetAxisRaw("DPad_Vertical") == -1 || Input.GetAxisRaw("Mouse ScrollWheel") < 0) && zoomtime >= zoomdelay)
{
if (state == "close")
{
state = "mid";
distance = 7.0F;
}
else if (state == "mid")
{
state = "far";
distance = 10.0F;
}
zoomtime = 0.0F;
}
if (target && camtime >= camdelay/2) {
x += Input.GetAxisRaw("Horizontal_R") * xSpeed * 0.02F;
y -= Input.GetAxisRaw("Vertical_R") * ySpeed * 0.02F;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * new Vector3(0.0F, 0.0F, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
if(Input.GetButtonDown("ThumbstickButton_R"))
{
camtime = 0;
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;
}
if (camtime <= camdelay)
{
camtime += Time.deltaTime;
// Calculate the current rotation angles
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime *2);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
Vector3 temp = transform.position;
temp.y = currentHeight;
transform.position = temp;
// Always look at the target
transform.LookAt (target);
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
}
static float ClampAngle (float angle, float min, float max) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
}
Update: Since then I have mostly fixed the collision and occlusion based problems. However the resetting camera continues to jump. I may have to simply remove that functionality if we cannot find a solution.