Skip to content

Instantly share code, notes, and snippets.

@gonztirado
Created November 16, 2017 12:44
Show Gist options
  • Save gonztirado/46fd1f980d532efc4ccf7f4c947c58fa to your computer and use it in GitHub Desktop.
Save gonztirado/46fd1f980d532efc4ccf7f4c947c58fa to your computer and use it in GitHub Desktop.
Sigue la camara al personaje manteniendo siempre un desplazamiento con respecto a donde está avanzando
using UnityEngine;
using System.Collections;
public class CameraFollower : MonoBehaviour
{
public Transform target;
public float initOffsetX;
public float initOffsetY;
public float moveToRightOffset;
public float moveToLeftOffset;
public float moveToBottomOffset;
public float moveOffset;
public float minCamaraPositionX;
public float minCamaraPositionY;
private float xOffset;
private float yOffset;
private void Awake()
{
this.xOffset = initOffsetX;
this.yOffset = initOffsetY;
}
void Update()
{
float newPositionX = target.position.x + xOffset;
float newPositionY = target.position.y + yOffset;
if (newPositionX < minCamaraPositionX)
newPositionX = minCamaraPositionX;
if (newPositionY < minCamaraPositionY)
newPositionY = minCamaraPositionY;
transform.position = new Vector3(newPositionX, newPositionY, transform.position.z);
}
public void FollowUpOffset()
{
if (moveToBottomOffset < this.yOffset)
{
this.xOffset -= moveOffset;
if (this.yOffset < moveToBottomOffset)
this.yOffset = moveToBottomOffset;
}
}
public void MoveToLeftOffset()
{
if (moveToLeftOffset < this.xOffset)
{
this.xOffset -= moveOffset;
if (this.xOffset < moveToLeftOffset)
this.xOffset = moveToLeftOffset;
}
}
public void MoveToRightOffset()
{
if (moveToRightOffset > this.xOffset)
{
this.xOffset += moveOffset;
if (this.xOffset > moveToRightOffset)
this.xOffset = moveToRightOffset;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment