Question by
fayeed · Nov 22, 2015 at 05:52 PM ·
c#camera2d gamecamera-lookcamera follow
camera not following target after reload
so I am using this camera script to follow the target but after i reload the game camera doesn't follow the target here's the script
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()
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
target = player.transform;
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).y;
bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
if (updateLookAheadTarget)
{
lookAheadPos = lookAheadFactor * Vector3.up * 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
Please press the 101010 button on an empty line so that you get the popup window. Paste and format your code there. This is quire unreadable :(
@fayeed Better, thanks. Good enough to make sense out of it. There's room for improvement but it's reasonably within the standard seen on this user group.
Your answer
Follow this Question
Related Questions
Camera Jitters When Displacing and Rotating Smoothly 0 Answers
CS1216 error 0 Answers
Make Camera follow rotation of 2 Players 1 Answer
switch camera to follow from a character to another one 1 Answer