0
\$\begingroup\$

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);
    }
}
```
\$\endgroup\$
  • \$\begingroup\$ did you try to slove your problem with colliders? \$\endgroup\$ – Stranger in the Q Apr 25 at 7:20
  • \$\begingroup\$ I haven't, but I'll try looking into them. Thanks for the suggestion. \$\endgroup\$ – DustyShinigami Apr 25 at 10:17
0
\$\begingroup\$

when clamping the camera to your bounds you want to make sure to re-assign the position like so

-added these in case you wanted to setup and test in inspector but you can set however needed

 public Vector3 MinCamPosition;
 public Vector3 MaxCamPosition;


void Update()
{
    transform.position = new Vector3(Mathf.Clamp(transform.position.x, MinCamPosition.x, MaxCamPosition.x),
                Mathf.Clamp(transform.position.y, MinCamPosition.y, MaxCamPosition.y), 
                Mathf.Clamp(transform.position.z, MinCamPosition.z, MaxCamPosition.z)); 
}
\$\endgroup\$
  • \$\begingroup\$ You can use Vector3.Min / Vector3.Max to express this a little more concisely. \$\endgroup\$ – DMGregory Apr 25 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.