Hi, fellow unity developers
I wrote a script for a RTS camera that prevents the camera from going off the terrain (when the player moves the camera with WASD / arrow keys, found in another script, not included below).
As you can see, in my code below, I have hardcoded the width and height of the terrain.
I want to be able to automatically detect the width and height of the terrain.
Note: I am very new to Unity and C# so if you know the answer, please provide a complete code not just fragments, ty
using System.Collections;
using UnityEngine;
public class CameraBounder : MonoBehaviour {
int minXBound = 50;
int maxXBound = 50;
int minZBound = 100;
int maxZBound = 200;
int terrainWidth = 500;
int terrainHeight = 500;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log("X: " + transform.position.x + " Z: " + transform.position.z);
if (transform.position.x > terrainWidth - maxXBound) {
transform.position = new Vector3 ((terrainWidth-maxXBound), transform.position.y, transform.position.z);
}
if (transform.position.x < minXBound) {
transform.position = new Vector3 (minXBound, transform.position.y, transform.position.z);
}
if (transform.position.z > terrainHeight - maxZBound) {
transform.position = new Vector3 (transform.position.x, transform.position.y, (terrainHeight-maxZBound));
}
if (transform.position.z < minZBound) {
transform.position = new Vector3 (transform.position.x, transform.position.y, minZBound);
}
}
}