I'm using a modified version of the OrbitCamera from the 3rd person platformer tutorial. I'm trying to achieve a lock-on effect where the camera orbits around the player by default, but if you look at an object tagged enemy and tap the right mouse button, the camera position switches to the position of an empty gameobject positioned behind the player's shoulder called lockOnPos, and the camera rotation is locked to the player rotation. If you click the right mouse button again, you unlock, and it goes back to the orbit camera.
As you'll see in my code, I achieved this by using a coroutine to constantly poll whether or not "lockOnToggle" is true, and I toggle it by the Fire2 button (right mouse). If it is true, in Update, I cache the position of the camera as it was in orbit in a variable called prevCamPos, then switch the camera position to the position of the lockOnPos object, and lock the camera rotation to the player object. Otherwise, I check to see if prevCamPos was set in the previous update, and if it was, set the camera position back to prevCamPos, and then do the normal OrbitCamera stuff.
This works fine, but I don't like the way it snaps immediately, so I tried lerping the camera positions when I do the switches, and its completely jittery and weird, and I can't figure out why.
Anyway, here's the bulk of my code:
function Update () {
if (player) {
if(lockOnToggle == true)
{
prevCamPos = transform.position;
player.gameObject.GetComponent(PlayerLockOn).LockOn(enemyPos);
transform.position = lockOnPos.position;
transform.rotation = player.rotation;
}
else
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var targetPos = player.position + targetOffset;
var direction = rotation * -Vector3.forward;
var targetDistance = AdjustLineOfSight(targetPos, direction);
currentDistance = Mathf.SmoothDamp(currentDistance, targetDistance, distanceVelocity, closerSnapLag * .3);
transform.rotation = rotation;
newCamPos= targetPos + direction * currentDistance;
if(prevCamPos == Vector3.zero)
transform.position = newCamPos;
else{
LerpPosition(0.5, prevCamPos, newCamPos);
prevCamPos = Vector3.zero;
}
}
}
}
function LerpPosition(time : float, prevPos : Vector3, newPos : Vector3) { var originalTime = time;
while (time > 0.0)
{
time -= Time.deltaTime;
transform.position = Vector3.Lerp(newPos, prevPos, time / originalTime);
yield;
}
}
Any idea what I'm doing wrong?