using System;
using UnityEngine;
namespace Framework.Cameras {
/// Controls a camera in a 2.5D platformer
public class PlatformerCameraController : CameraController {
/// Height adjustment of the camera
public float ExtraHeight = 1.0f;
/// Distance from which the camera observes the target
public float Distance = 8.0f;
/// Called once per physics frame
protected void FixedUpdate() {
if(base.Target != null) {
this.targetPosition = base.Target.position;
}
Vector3 adjustedTargetPosition = this.targetPosition;
adjustedTargetPosition.y += this.ExtraHeight;
adjustedTargetPosition.z -= this.Distance;
Vector3 newPosition = transform.position;
{
float elapsedTime = Time.fixedDeltaTime;
while(elapsedTime > 0.01f) { // Poor man's integration
newPosition += (adjustedTargetPosition - newPosition) / 8.0f;
elapsedTime -= 0.01f;
}
newPosition += (adjustedTargetPosition - newPosition) / 8.0f * (elapsedTime * 100.0f);
}
transform.position = newPosition;
}
/// Position of the camera's current or latest valid target
protected Vector3 TargetPosition {
get { return this.targetPosition; }
}
/// Current position of the camera's target
private Vector3 targetPosition;
}
} // namespace Framework.Cameras