private float getFixedVsVelocityFactor(float speedCoeff) { // If the car isn't moving, default to looking forwards if (veh.Velocity.Magnitude() < nearlySttopedSpeed) { return(0f); } else { // for bikes, always look at velocity if (isCycleOrByke) { return(1f); } // if veh is in air, look at is velocity if (veh.IsInAir || veh.CurrentGear == 0) { return(1f); } else // if is on the ground. fit camera (mostly) behind vehicle (for better drift control) { // Different factors for low/high speed return(MathR.Lerp(velocityInfluenceLowSpeed, velocityInfluence, MathR.Clamp(speedCoeff / lowSpeedLimit, 0f, 1f))); } } }
private Transform UpdateCameraRear() { // smooth out current rotation and position currentRotation = MathR.EulerNlerp(currentRotation, rearCamCurrentTransform.quaternion, (responsivenessMultiplier * generalMovementSpeed) * Time.getDeltaTime()); currentPos = Vector3.Lerp(currentPos, rearCamCurrentTransform.position, MathR.Clamp01((responsivenessMultiplier * generalMovementSpeed) * Time.getDeltaTime())); Quaternion look; var wantedPos = veh.Position + (veh.ForwardVector * finalDummyOffset); // If veh has towed vehicle, increase camera distance by towed vehicle longitude // should camera be attached to vehicle's back or back his velocity var fixedVsVelocity = getFixedVsVelocityFactor(speedCoeff); Quaternion vehQuat = veh.Quaternion; // Compute camera position rear the vechicle var wantedPosFixed = wantedPos - veh.Quaternion * Vector3.RelativeFront * fullLongitudeOffset; // smooth out velocity // Compute camera postition rear the direction of the vehcle if (speedCoeff >= stoppedSpeed) { wantedPosVelocity = wantedPos + MathR.QuaternionLookRotation(smoothVelocity) * Vector3.RelativeBottom * fullLongitudeOffset; } // Smooth factor between two above cam positions smoothFixedVsVelocity = MathR.Lerp(smoothFixedVsVelocity, fixedVsVelocity, (fixedVsVelocitySpeed) * Time.getDeltaTime()); if (!isCycleOrByke) { tempSmoothVsVl = MathR.Lerp(tempSmoothVsVl, MathR.Clamp(speedCoeff * 0.4347826f, 0.025f, 1f), (fixedVsVelocitySpeed * 0.05f)); smoothFixedVsVelocity = MathR.Lerp(0f, smoothFixedVsVelocity, tempSmoothVsVl); } wantedPos = Vector3.Lerp(wantedPosFixed, wantedPosVelocity, MathR.Clamp01(smoothFixedVsVelocity)); //currentPos = Vector3.Lerp(currentPos, wantedPos, Mathr.Clamp01((responsivenessMultiplier * cameraStickiness) * Time.getDeltaTime())); currentPos = Vector3.Lerp(currentPos, wantedPos, MathR.Clamp01((responsivenessMultiplier * cameraStickiness) * Time.getDeltaTime())); //rearCamCurrentTransform.position = currentPos; targetCamera.PointAt(pointAt); look = Quaternion.Euler(targetCamera.Rotation); // Rotate the camera towards the velocity vector. var finalCamRotationSpeed = MathR.Lerp(cameraRotationSpeedLowSpeed, cameraRotationSpeed, ((speedCoeff / lowSpeedLimit) * 1.32f) * Time.getDeltaTime() * 51f); look = MathR.EulerNlerp(currentRotation, look, (1.8f * finalCamRotationSpeed) * Time.getDeltaTime()); // Fix stuttering (mantain camera distance fixed in local space) Transform fixedDistanceTr = new Transform(currentPos + fullHeightOffset + (Vector3.WorldUp * extraCamHeight), Quaternion.Identity); fixedDistanceTr.PointAt(pointAt); Quaternion finalQuat = fixedDistanceTr.quaternion; if (script.isMouseLooking) { finalQuat = Quaternion.Lerp(finalQuat, getFreelookQuaternion(), script.smoothIsFreeLooking); } fixedDistanceTr.position = veh.Position + fullHeightOffset + (Vector3.WorldUp * extraCamHeight) + (finalQuat * Vector3.RelativeBack * (fullLongitudeOffset + currentDistanceIncrement)); //fixedDistanceTr.position = fixedDistanceTr.position + fullHeightOffset; var transform = new Transform(); transform.position = fixedDistanceTr.position; //transform.position = currentPos + fullHeightOffset; transform.rotation = look.ToEulerAngles(); transform.quaternion = look; rearCamCurrentTransform = transform; return(transform); }