Like the title says, I’m after some kind of constraint for the camera to prevent the player from seeing beyond the scene and seeing the Skybox. I came across the code to add a Mathf.Clamp
, but I can’t get it to work.
I’m currently trying to make a 2.5D platformer in which the camera follows the player (ahead), which leads to seeing beyond the scene to the far left and right. I already have an invisible wall in place to prevent the player from falling off, but I would love to also clamp the camera from going any further.
This is the script I have at the moment:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
(SerializeField) GameObject player = null;
(SerializeField) (Range(0.1f, 2f)) float followAhead = 2f;
(SerializeField) (Range(0.1f, 2f)) float smoothing = 1f;
//public PlayerController playerController;
private float minPosition = -9.37f;
private float maxPosition = 4.68f;
private Vector3 cameraClamp;
const float m_minY = 2f;
Vector3 targetPosition;
Vector3 cameraOffset;
void Start()
{
cameraOffset = transform.position - player.transform.position;
}
void Update()
{
targetPosition = (player.transform.position + (player.transform.forward * followAhead)) + cameraOffset;
//smoothedForward = Vector3.MoveTowards(smoothedForward, playerController.GetTravelDirection(), 0.5f * Time.deltaTime);
//targetPosition = (player.transform.position + (smoothedForward * followAhead));
//transform.position = player.transform.position + cameraOffset;
targetPosition.y = Mathf.Min(targetPosition.y, m_minY);
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.deltaTime);
cameraClamp.x = Mathf.Clamp(transform.position.x, -9.37f, 4.68f);
}
}
```