[EDIT] : I fixed the camera so that it doesnāt rely on a frame by frame multiplier (I found it was slowing down the movement of the camera as usage got heavy. It now simply works by making the current rotation the same value as the y-position value, within the bounds you set via the editor. You can of course make it (transform.position.y +/- yourValue) if you think a little offset might look good.
I pasted the new code below, and Iāll leave the old file for reference. The one you want is CameraControlFixed.cs
The only bug is if your panSpeed is so ridiculously high that your camera goes beyond the bounds before a frame can even check it.
For anybody thatās still looking in here, I made this simple Camera Script with limitations and rotation as you go in and out using āRā and āFā.
Pretty much all the necessary fields can be set from within the editor as you play around with it in runtime to figure out what looks good
using UnityEngine;
public class CameraControlFixed : MonoBehaviour
{
public float panSpeed = 30f;
public float windowEdgeBorder = 10f;
private float cameraYRot;
private float cameraZRot;
[Header("CameraLimits")]
public float leftLimit = 0f;
public float rightLimit = 70f;
public float upLimit = 65f;
public float downLimit = -4f;
public float zoomInLimit = 8f;
public float zoomOutLimit = 55f;
public float xRotationLowerLimit = 10f;
public float xRotationUpperLimit = 90f;
void Start() {
cameraYRot = transform.rotation.y;
cameraZRot = transform.rotation.z;
}
// Update is called once per frame
void Update()
{
float mouseY = Input.mousePosition.y;
float mouseX = Input.mousePosition.x;
/*
Camera controls in this next condition after we
check that the mouse is within the window
*/
if (mouseX >= 0 && mouseX <= Screen.width &&
mouseY >= 0 && mouseY <= Screen.height)
{
/*
Below we are checking that
A.) a specific key is pressed
B.) in some cases (left, right, back, forward) that the mouse is within the
edges of the screen via the border and Screen attributes
C.) the camera's current position is not past the set limits
Then we Translate that camera toward a certain direction relative to the
World Space
*/
// up with 'w'
if ((Input.GetKey("w") || mouseY >= Screen.height - windowEdgeBorder) &&
transform.position.z < upLimit) {
transform.Translate(Vector3.forward * panSpeed * Time.deltaTime,
Space.World);
}
// left with 'a'
if ((Input.GetKey("a") || mouseX <= windowEdgeBorder) &&
transform.position.x > leftLimit) {
transform.Translate(Vector3.left * panSpeed * Time.deltaTime,
Space.World);
}
// down with 's'
if ((Input.GetKey("s") || mouseY <= windowEdgeBorder) &&
transform.position.z > downLimit) {
transform.Translate(Vector3.back * panSpeed * Time.deltaTime,
Space.World);
}
// right with 'd'
if ((Input.GetKey("d") || mouseX >= Screen.width - windowEdgeBorder) &&
transform.position.x < rightLimit) {
transform.Translate(Vector3.right * panSpeed * Time.deltaTime,
Space.World);
}
// zoom in
if (Input.GetKey("r") && transform.position.y > zoomInLimit) {
transform.Translate(Vector3.down * panSpeed * Time.deltaTime, Space.World);
//transform.Rotate(-rotationPivotFactor* rotationSpeedPerFrame, 0f, 0f, Space.Self);
if (transform.position.y > xRotationLowerLimit && transform.position.y < xRotationUpperLimit)
transform.rotation = Quaternion.Euler( transform.position.y, cameraYRot, cameraZRot);
}
// zoom out
if (Input.GetKey("f") && transform.position.y < zoomOutLimit) {
transform.Translate(Vector3.up * panSpeed * Time.deltaTime, Space.World);
//transform.Rotate(rotationPivotFactor* rotationSpeedPerFrame, 0f, 0f, Space.Self);
if (transform.position.y > xRotationLowerLimit && transform.position.y < xRotationUpperLimit)
transform.rotation = Quaternion.Euler( transform.position.y, cameraYRot, cameraZRot);
}
}
}
}
[EDIT 2] : Looks like I was missing this too. I had it in my project but forgot to upload. Donāt credit me for this though, I forget where I got it⦠Has to do with Screen Size and how the Camera changes accordingly
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class ScreenSize : MonoBehaviour
{
// Set this to the in-world distance between the left & right edges of your scene.
public float sceneWidth = 10;
Camera _camera;
void Start()
{
_camera = GetComponent<Camera>();
}
// Adjust the camera's height so the desired scene width fits in view
// even if the screen/window size changes dynamically.
void Update()
{
float unitsPerPixel = sceneWidth / Screen.width;
float desiredHalfHeight = 0.5f * unitsPerPixel * Screen.height;
_camera.orthographicSize = desiredHalfHeight;
}
}
Still new to 3-d programming, from what I can tell it works consistently, but maybe Iām missing something. Although the camera moves on the Y-Axis with Time.deltaTime, the rotation is based on each frame because I figured it would need a consistent multiplier
4586815ā427567āCameraControl.cs (3.22 KB)
4586815ā432307āCameraControlFixed.cs (3.88 KB)
4586815ā432484āScreenSize.cs (714 Bytes)