Hello All,
I have a script below that has the camera follow my sprite , thinking about having an algo to make my char die if it falls out of the camera , my idea would be is to have the clamp update its Ymin , with current Ymin position and lock it , inorder for my character to fall out of camera view.
heres my current script for the camera follow .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class followCam : MonoBehaviour
{
public GameObject Player;
public Transform target;
public float smoothSpeed = 0.135f;
public Vector3 offset;
private Vector3 velocity = Vector3.zero;
//enable and set maximum y value
public bool YMaxEnabled = false;
public float YMaxValue = 0;
//enable and set minimum y value
public bool YMinEnabled = false;
public float YMinValue = 0;
//enable and set maximum x value
public bool XMaxEnabled = false;
public float XMaxValue = 0;
//enable and set minimum x value
public bool XMinEnabled = false;
public float XMinValue = 0;
private void Start()
{
// referencing the child actiuvated
target = CharacterSelection.Instance.Player.transform;
}
void LateUpdate()
{
Vector3 desiredPosition = target.position + offset;
//Vertical manipulation, Limit
if (YMinEnabled && YMaxEnabled)
desiredPosition.y = Mathf.Clamp(target.position.y, YMinValue, YMaxValue);
else if (YMinEnabled)
desiredPosition.y = Mathf.Clamp(target.position.y, YMinValue, target.position.y);
else if(YMaxEnabled)
desiredPosition.y = Mathf.Clamp(target.position.y, target.position.y, YMaxValue);
//Horizontal manipulation, Limit
if (XMinEnabled && XMaxEnabled)
desiredPosition.x = Mathf.Clamp(target.position.x, XMinValue, XMaxValue);
else if (XMinEnabled)
desiredPosition.x = Mathf.Clamp(target.position.x, XMinValue, target.position.x);
else if (XMaxEnabled)
desiredPosition.x = Mathf.Clamp(target.position.x, target.position.x, XMaxValue);
Vector3 smoothPosition = Vector3.SmoothDamp(transform.position, desiredPosition, ref velocity, smoothSpeed);
transform.position = smoothPosition;
}
}