- Home /
Camera Movement and Zooming limit,
Did anyone knows of how to limit the Z axis? I use this as a camera control And the thing I want to know is how can I limit the zoom of Z axis? Like..! If Z Axiz has "lower than -125" And the thing I want to do is stop zooming there! Did anyone know? Thank you!
using UnityEngine;
public class OrbitalKey : MonoBehaviour
{
public int Speed = 2;
void Update()
{
float xAxisValue = Input.GetAxis("Horizontal") / Speed;
float zAxisValue = Input.GetAxis("Vertical") * Speed;
transform.position = new Vector3(transform.position.x + xAxisValue, transform.position.y, transform.position.z + zAxisValue);
}
}
Answer by Totoh899 · Feb 03, 2018 at 02:40 PM
Thank you so much! @vatsav-gundigara But one thing tho! The script you wrote is not 100% correct.. Lemme fix that a little
using UnityEngine;
public class CameraScript : MonoBehaviour {
public int Speed = 2;
public float zoomlimit = -125;
void Update()
{
float xAxisValue = Input.GetAxis("Horizontal") / Speed;
float zAxisValue = Input.GetAxis("Vertical") * Speed;
float zoomDistance = transform.position.z + zAxisValue;
if (zoomDistance > zoomlimit ) {
transform.position = new Vector3 (transform.position.x + xAxisValue, transform.position.y, zoomDistance);
}
}
}
It should be from "<" to ">" But hey! Thank you so much for your help! Now I'm learning more from coding!
Out Zoom and In Zoom
using UnityEngine;
public class Orbital$$anonymous$$ey : $$anonymous$$onoBehaviour {
public int Speed = 1;
public float zoomlimit = -125;
public float zoomlimitIn = -20;
void Update()
{
float xAxisValue = Input.GetAxis("Horizontal") / Speed;
float zAxisValue = Input.GetAxis("Vertical") * Speed;
float zoomDistance = transform.position.z + zAxisValue;
if (zoomDistance > zoomlimit && zoomDistance < zoomlimitIn)
{
transform.position = new Vector3(transform.position.x + xAxisValue, transform.position.y, zoomDistance);
}
}
}
Answer by vatsav-gundigara · Feb 03, 2018 at 10:45 AM
public class CameraScript : MonoBehaviour {
public int Speed = 2;
public float zoomlimit = -125;
void Update()
{
float xAxisValue = Input.GetAxis("Horizontal") / Speed;
float zAxisValue = Input.GetAxis("Vertical") * Speed;
float zoomDistance = transform.position.z + zAxisValue;
if (zoomDistance < zoomlimit ) {
transform.position = new Vector3 (transform.position.x + xAxisValue, transform.position.y, zoomDistance);
}
}
}
@Totoh899 Reffer this code