- Home /
Stop 2D camera X axis movement if equal to one of two coordinates
This camera script follows a transform (in this case th eplayer), but at the end of each side of the level i would like it to stop X movement if the transform it follows is at a certain position (to stop camera tracking off the level).
Here is the current script.
using UnityEngine;
using System.Collections;
public class Camera2DFollow : MonoBehaviour {
public Transform target;
public float damping = 1;
public float lookAheadFactor = 3;
public float lookAheadReturnSpeed = 0.5f;
public float lookAheadMoveThreshold = 0.1f;
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;
}
// 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;
}
}
Comment
Answer by Jeffuk · Oct 09, 2014 at 12:01 PM
Is it as simple as wrapping everything in the update() method with
if (target.position.x>min && target.position.x<max){
...
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Change the background color attribute of a camera in C#? 2 Answers
Camera Script - Bird's Eye View 1 Answer
Screen Points to construct a Rect (Not axis aligned) 0 Answers
Jump & Animation Script 2D 0 Answers