- Home /
c# scrolling zoom, lock X&Y in current position?
I am using a main camera to zoom into my gameObject using the scroll wheel. I've got the script working, but when I play, the main camera x and y values snap to 0 instead of staying where they are located in space. I need it a little off center for my GUI panel. How do I keep the camera position values at x = 0.28, y = -0.2 while I zoom?
public class DirectionalButtons : MonoBehaviour
{
public float distance;
private float sensitivityDistance = -7.5f;
private float damping = 2.5f;
private float min = -4;
private float max = -2;
private Vector3 zdistance;
void Start ()
{
distance = -3f;
distance = transform.localPosition.z;
}
void Update ()
{
distance -= Input.GetAxis("Mouse ScrollWheel") * sensitivityDistance;
distance = Mathf.Clamp(distance, min, max);
zdistance.z = Mathf.Lerp(transform.localPosition.z, distance, Time.deltaTime * damping);
transform.localPosition = zdistance;
}
}
Answer by ArkaneX · Sep 12, 2013 at 06:48 PM
The problem is, that you assign a zdistance vector, which by default have x and y set to 0. You have to use actual x and y from localPosition, by reading current local position, modifying z value, and then assigining it back:
distance -= Input.GetAxis("Mouse ScrollWheel") * sensitivityDistance;
distance = Mathf.Clamp(distance, min, max);
zdistance = transform.localPosition; // <- reading local position
zdistance.z = Mathf.Lerp(transform.localPosition.z, distance, Time.deltaTime * damping);
transform.localPosition = zdistance;
Answer by Saucy_Noodle · Sep 12, 2013 at 08:15 PM
Here's how I fixed it:
{
public float distance;
private float sensitivityDistance = -7.5f;
private float damping = 2.5f;
private float min = -4;
private float max = -2;
private Vector3 zdistance;
void Start ()
{
distance = -3f;
distance = transform.localPosition.z;
}
void Update ()
{
distance -= Input.GetAxis("Mouse ScrollWheel") * sensitivityDistance;
distance = Mathf.Clamp(distance, min, max);
zdistance.z = Mathf.Lerp(transform.localPosition.z, distance, Time.deltaTime * damping);
transform.localPosition = zdistance;
Vector3 pos = transform.position;
pos.x = .2f;
pos.y = -.15f;
transform.position = pos;
}
}
I believe my answer was a valid one. You wanted to keep position of the camera, and that's exactly what code from my answer do. You didn't mention anything about requirement to be able to keep x and y in world position, only local position was present in script.
If you agree, please consider accepting my answer. If you disagree, please write what is wrong with it.
I had found this solution before you responded. Both work. Yours is easier.
Thanks for accepting. As an additional info - your solution is going to work if world position of camera is equal to its local position. If camera would be a child of some other object, then things could get messy.
That makes sense. Thanks! I appreciate the help! There is so much to this software, it's easy to get lost.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114031im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast Trouble 1 Answer
How can I increase the view distance of these trees leaves? (billboarding) 0 Answers
Demons Souls Fall death Camera 1 Answer
How can I scale the Joystick and TouchPad? 1 Answer
continuous time in collider box 2 Answers