I’ve been messing around with Camera2DFollow in the new 2D Sample Assets Unity put on the Asset Store and I’m trying to make the camera switch focus to the ball after the robot sprite touches it. Any advice would be appreciated.
public class Camera2DFollow : MonoBehaviour {
public Transform target;
public float damping = 1;
public float lookAheadFactor = -5;
public float lookAheadReturnSpeed = 0.1f;
public float lookAheadMoveThreshold = 0.5f;
public Transform newTarget;
float offsetZ;
Vector3 lastTargetPosition;
Vector3 currentVelocity;
Vector3 lookAheadPos;
// Use this for initialization
void Start () {
lastTargetPosition = target.position;
offsetZ = (transform.position - target.position).z;
transform.parent = null;
// newTarget = GameObject.Find("AsteroidSprite");
}
// Update is called once per frame
void Update () {
// only update lookahead pos if accelerating or changed direction
float xMoveDelta = (target.position - lastTargetPosition).x;
bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
if (updateLookAheadTarget) {
lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
} else {
lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);
}
Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
transform.position = newPos;
lastTargetPosition = target.position;
}
void switchTarget(){
if(target.collider2D == newTarget.collider2D){
target = newTarget;
}
}
}