I’ve been following a tutorial and everythings been working fine exept the player can go out of bounds, the camera has been set to not go out of bounds. This is the player Movement script
using Newtonsoft.Json.Bson;
using UnityEngine;
using UnityEngine.Tilemaps;
public class PlayerMovement : MonoBehaviour
{
// Declaring variables
// Private means you have to decalre it in the script
// Public means the object can be placed in the field in the editor, it can be declared in the script
// Private is not visible in the editor
// Public is visible in the editor
// Used to change the movement speed. '[SerializeField]' is used to change variable in editor
[SerializeField] private float moveSpeed;
// calls Rigidbody and sets it varaible to rb
private Rigidbody2D rb;
// Referencing this script thats on the player and calling it instance
// Static means there can only be one version of this
// This is accessible anywhere
public static PlayerMovement instance;
public string areaTransitionName;
private Vector3 bottomLeftLimit;
private Vector3 topRightLimit;
// This happens before start
private void Awake()
{
// Checks if instance is empty
if (instance == null)
{
// setting instance value to this player
instance = this;
}
else
{
// if instance isn't empty destroy the new game object created
Destroy(gameObject);
}
}
private void Start()
{
// stores the Rigidody component in the varaible rb
rb = GetComponent<Rigidbody2D>();
// When we load a new scene dont destroy the game object that this script is attached to
DontDestroyOnLoad(gameObject);
// Keep the centeral point of the camera inside the bounds
// Mathf is math function
// Clamp means to keep something always between two points, x and y, z is only included to be able to see everything by being further away
transform.position = new Vector3(Mathf.Clamp(transform.position.x, bottomLeftLimit.x, topRightLimit.x), Mathf.Clamp(transform.position.y, bottomLeftLimit.y, topRightLimit.y), transform.position.z);
}
private void Update()
{
// Checks for horizontal or vertical keys pressed, as seen in the unity input system
// The movement velocity is timed by the set movement speed
rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * moveSpeed;
}
// Creating functions
// Have to give a bottom left value and top right variable for the function to happen
public void SetBounds(Vector3 botLeft, Vector3 topRight)
{
// Setting bottom left limit to what was put in the brackets in the camera controller script
bottomLeftLimit = botLeft + new Vector3(1f, 1f, 0f);
// Setting rop right limit to what was put in the brackets in the camera controller script and adding a bit so the player isnt half outside the map
topRightLimit = topRight + new Vector3(-1f, -1f, 0f);
}
}
And this is the Camera Controller Script
using UnityEngine;
using UnityEngine.Tilemaps;
public class CameraController : MonoBehaviour
{
public Transform target;
// In editor set this to the largest tile map
public Tilemap map;
private Vector3 bottomLeftLimit;
private Vector3 topRightLimit;
// Getting half of the height and width of the camera
// Going to be used because only half of the camera is out, this is because the later code only keeps the centeral point of the camera inside the bounds
private float halfHeight;
private float halfWidth;
private void Start()
{
// Setting the target to the Player
target = FindObjectOfType<PlayerMovement>().transform;
// Checks what is the current height of the camera and stores it in halfHeight
halfHeight = Camera.main.orthographicSize;
// Calculating half the width of the camera
halfWidth = halfHeight * Camera.main.aspect;
// The bottom left limit is the tile with the lowest x and y values
// On the bottom left limit half the width of the camera is being added to the x value and half the height of the camera is being added to the y value, z is being left alone - kept the same
bottomLeftLimit = map.localBounds.min + new Vector3(halfWidth, halfHeight, 0f);
// The top right limit is the tile with the highest x and y values
// On the bottom left limit negative half the width of the camera is being added to the x value and negative half the height of the camera is being added to the y value, z is being left alone - kept the same
topRightLimit = map.localBounds.max + new Vector3(-halfWidth, -halfHeight, 0f);
// Settings the bounds for the player to the lowest x and y values of the tile map and the highest values of the x and y values of the tile map
PlayerMovement.instance.SetBounds(map.localBounds.min, map.localBounds.max);
}
// LateUpdate is called once per frame after the Update funciton has happened, used so it doesnt lag behind player
private void LateUpdate()
{
// Set the position of the object this script is attached to, to the targets position
transform.position = new Vector3(target.position.x, target.position.y, transform.position.z);
// Keep the centeral point of the camera inside the bounds
// Mathf is math function
// Clamp means to keep the camera always between two points, x and y, z is only included to be able to see everything by being further away
transform.position = new Vector3(Mathf.Clamp(transform.position.x, bottomLeftLimit.x, topRightLimit.x), Mathf.Clamp(transform.position.y, bottomLeftLimit.y, topRightLimit.y), transform.position.z);
}
}
There is notes hopefully explaining what everything does. When the player moves so does the camera, the camera doesn’t go outside the map but the player does. I’m guessing its a logic error as there is no errors in the editor. I’m not sure what is going wrong, the only thing I could think of is that I have messed up the function. Any help is appreciated!