Reputation: 598
My perspective camera is rotated on the x axis. I've implemented a zoom with the scroll wheel and it works great.
I'm trying to limit the zoom, however. My solution right now is to take the relative scale of the transform.forward
y and z components and clamp the y and z position of the camera by using Mathf.Clamp
and multiplying the minZoom
and maxZoom
floats by the corresponding transform.forward
component. It works well, except it limits the y axis, so when I translate around I can't go below a given y due to my clamp with is ideally meant to affect the zoom only.
Is there a way to clamp the forward and backward movement of the camera? I've been looking everywhere but can't find a thing.
Thanks.
Edit
Here's the relevant code (my clamping occurs in LateUpdate):
public class ClickManager : MonoBehaviour
{
public float dragSpeed = 2;
public float minZoom = 0.5f;
public float maxZoom = 5.0f;
private Vector3 dragOrigin;
private float lerpPercentBack = 0; // value between 0 and 1 representing percentage of lerp
private float lerpPercentForward = 0;
private bool isLerpingBack = false;
private bool isLerpingForward = false;
void Update()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (Input.GetMouseButtonDown(1))
{
dragOrigin = Input.mousePosition;
return;
}
if (scroll > 0)
{
isLerpingForward = true;
lerpPercentForward = 0;
}
if (scroll < 0)
{
isLerpingBack = true;
lerpPercentBack = 0;
}
if (isLerpingBack)
{
scrollLerperBack();
lerpPercentBack += 0.05f;
if (lerpPercentBack >= 1.0f)
{
lerpPercentBack = 0;
isLerpingBack = false;
}
}
if (isLerpingForward)
{
scrollLerperForward();
lerpPercentForward += 0.05f;
if (lerpPercentForward >= 1.0f)
{
lerpPercentForward = 0;
isLerpingForward = false;
}
}
//Debug.Log(lerpPercent);
/*if (!Input.GetMouseButton(1)) return;
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
Vector3 move = new Vector3(pos.x * dragSpeed, pos.y * dragSpeed, 0);
transform.Translate(move, Space.World);
Debug.Log(scroll);*/
}
void scrollLerperBack()
{
transform.position -= Vector3.Lerp(transform.forward, Vector3.zero, lerpPercentBack);
}
void scrollLerperForward()
{
transform.position += Vector3.Lerp(transform.forward, Vector3.zero, lerpPercentForward);
}
void LateUpdate()
{
Vector3 pos = transform.position;
pos.y = Mathf.Clamp(pos.y, minZoom * transform.forward.y,
maxZoom * transform.forward.y);
pos.z = Mathf.Clamp(pos.z, minZoom * transform.forward.z,
maxZoom * transform.forward.z);
transform.localPosition = pos;
}
}
Upvotes: 1
Views: 1188
Reputation: 10720
Working Code:
using UnityEngine;
using System.Collections;
public class ClickManager : MonoBehaviour
{
public float dragSpeed = 2;
public float minZoom = 0.5f;
public float maxZoom = 5.0f;
private Vector3 dragOrigin;
private float lerpPercentBack = 0;
private float lerpPercentForward = 0;
private bool isLerpingBack = false;
private bool isLerpingForward = false;
Vector3 forwardPos;
Vector3 backwardPos;
void Start()
{
forwardPos = transform.position ;
forwardPos.z += maxZoom;
backwardPos = transform.position ;
backwardPos.z += minZoom;
}
void Update()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (Input.GetMouseButtonDown(1))
{
dragOrigin = Input.mousePosition;
return;
}
if (scroll > 0)
{
isLerpingForward = true;
lerpPercentForward = 0;
}
if (scroll < 0)
{
isLerpingBack = true;
lerpPercentBack = 0;
}
if (isLerpingBack)
{
scrollLerperBack();
lerpPercentBack += 0.05f;
if (lerpPercentBack >= 1.0f)
{
lerpPercentBack = 0;
isLerpingBack = false;
}
}
if (isLerpingForward)
{
scrollLerperForward();
lerpPercentForward += 0.05f;
if (lerpPercentForward >= 1.0f)
{
lerpPercentForward = 0;
isLerpingForward = false;
}
}
}
void scrollLerperBack()
{
transform.position = Vector3.MoveTowards(transform.position, backwardPos, Time.deltaTime * dragSpeed );
}
void scrollLerperForward()
{
transform.position = Vector3.MoveTowards(transform.position, forwardPos, Time.deltaTime * dragSpeed);
}
}
Upvotes: 1