- Home /
This post has been wikified, any user with enough reputation can edit it.
How can i put a camera border in this script?
using System; using UnityEngine;
namespace UnityStandardAssets._2D { 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;
private float m_OffsetZ;
private Vector3 m_LastTargetPosition;
private Vector3 m_CurrentVelocity;
private Vector3 m_LookAheadPos;
// Use this for initialization
private void Start()
{
m_LastTargetPosition = target.position;
m_OffsetZ = (transform.position - target.position).z;
transform.parent = null;
}
// Update is called once per frame
private void Update()
{
// only update lookahead pos if accelerating or changed direction
float xMoveDelta = (target.position - m_LastTargetPosition).x;
bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
if (updateLookAheadTarget)
{
m_LookAheadPos = lookAheadFactor*Vector3.right*Mathf.Sign(xMoveDelta);
}
else
{
m_LookAheadPos = Vector3.MoveTowards(m_LookAheadPos, Vector3.zero, Time.deltaTime*lookAheadReturnSpeed);
}
Vector3 aheadTargetPos = target.position + m_LookAheadPos + Vector3.forward*m_OffsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref m_CurrentVelocity, damping);
transform.position = newPos;
m_LastTargetPosition = target.position;
}
}
}
Comment
Answer by Mergster · Dec 08, 2016 at 09:31 PM
Take a look here: https://www.youtube.com/watch?v=u67fbxe8xxY
Your answer
![](https://koobas.hobune.stream/wayback/20220612102236im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Scale game vertically only 0 Answers
How to create a multilayered 2D effect? 1 Answer
Cinemachine camera shake on button press 0 Answers
Unity 3D Render Camera Bug!!! 1 Answer
Camera Smooth - Player moves little bit back when jump 1 Answer