Hi,
I wrote a script where the camera follows a vehicle, optimized for loopings, jumps and “wall-rides”.
using UnityEngine;
using System.Collections;
public class CamFollow : MonoBehaviour {
public Transform target;
public Vector3 targetOffset = new Vector3(0f, 1f, 0f);
public Vector3 checkGroundOffset = new Vector3(0f, -0.5f, 0f);
public float distance = 10;
public float angle = 20;
public float turnSpeed = 1;
public float detectGroundSpeed = 5;
float onGround;
Quaternion smoothRot;
void FixedUpdate() {
Vector3 targetPos = target.position;
Quaternion targetRot = target.rotation;
// is the target touching ground?
if (Physics.Raycast(targetPos + targetRot * checkGroundOffset, -target.up, 1f)) {
onGround = Mathf.Lerp(onGround, 1f, Time.deltaTime * detectGroundSpeed);
} else {
onGround = Mathf.Lerp(onGround, 0f, Time.deltaTime * detectGroundSpeed);
}
// align target rotation
smoothRot = Quaternion.Lerp(smoothRot, targetRot, turnSpeed * Time.fixedDeltaTime * onGround);
// move camera to offset position
transform.position = targetPos + smoothRot * targetOffset;
// look-at target
Vector3 cameraLook = targetPos + smoothRot * (targetOffset - new Vector3(0, angle, angle-90f));
// up-vector
float misalign = 2f*(1f-Vector3.Dot(Vector3.up, target.up));
Vector3 camUp = Vector3.Lerp(Vector3.up, target.up, misalign * onGround);
transform.LookAt(cameraLook, camUp);
// move to distance
transform.Translate(0, 0, -distance);
}
}
It works good so far, but I want that the helper rotation “smoothRot” aligns faster to the up-down movement than the left-right. So basically I want to somehow split this rotation into 2 parts to control each lerp speeds separately.
Maybe making a second helper rotation which aligns faster and than somehow combine both, but I’m not sure how exactly this could be achieved. I don’t think converting the Quaternions to Euler and than building an Euler from these would work, but any suggestion is appreciated.