I’ve been trying to make the camera work properly for some time now and i can’t find anything useful. I want to make a Super Mario-like camera follow, I’m using this code, and it’s working nicely for the x position but i wanted the threshold for y to be bigger, and i already tried to make one for y and one for x, because i dont want the player to be in the center of the camera, i only want the camera to go up or down in case the player goes really really close to the borders for y position… i even tried to use the script in an object and make it follow the player and child the camera to the object and it didnt go out as planned… depending on the speed the camera stutters too so if theres somethin i can do to make it smoother please help me! so basically i need a way to limit the y movement according to the player position where the player is never on the center of the camera and something to make the camera follow smoother.
public class CameraController : MonoBehaviour
{
[SerializeField]
Transform player;
private Vector3 moveTemp;
[SerializeField]
float speed = 4;
[SerializeField]
float xDifference;
[SerializeField]
float yDifference;
[SerializeField]
float threshold = 3;
void FixedUpdate()
{
if (player.transform.position.x > transform.position.x) {
xDifference = player.transform.position.x - transform.position.x;
} else {
xDifference = transform.position.x - player.transform.position.x;
}
if (player.transform.position.y > transform.position.y) {
yDifference = player.transform.position.y - transform.position.y;
} else {
yDifference = transform.position.y - player.transform.position.y;
}
if (xDifference >= threshold || yDifference >= threshold)
{
moveTemp = player.transform.position;
moveTemp.z = -17;
transform.position = Vector3.MoveTowards(transform.position, moveTemp, speed * Time.deltaTime);
}
}
}