- Home /
How to make camera zooming with a range? [C#]
Hey guys,
I am a very new user of Unity, but for my project at college I need to make a camera zooming with a range, so when some key button is pressed the camera zooms out for a certain range and then stops in zooming. But when the button is released, the camera moves back to the initial distance from the character.
I tried to make it, camera zooms out , but it doesn't stop, it's continuously moving backwards.
public GameObject Target;
public float OffsetY = 2.0f;
public float OffsetZ = 0.0f;
public float Offset;
public float OffsetZMax = 10.0f;
private bool ZoomOn = false;
public float SmoothTime = 0.075f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 destination = new Vector3(Target.transform.position.x, Target.transform.position.y + OffsetY, transform.position.z-Offset);
Vector3 velocity = Vector3.zero;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, SmoothTime);
if (Input.GetKey(KeyCode.F))
{
//OffsetZ += 1;
}
if (OffsetZ >= OffsetZMax)
{
OffsetZ = OffsetZMax;
}
Offset = OffsetZ;
}
I don't know to add the range to the zoom and zooming back to the initial position thing. Maybe someone knows how to do it? Or at least where I can find the solution for it? Thank you!
Your answer
Follow this Question
Related Questions
Problems making a smash bros like camera 2 Answers
ROBLOX Like Camera Script 2 Answers
Pinch Zoom and Pan while not calling other functions 1 Answer
Zooming in and out with an orthographic camera, while the bottom edge is fixed. 0 Answers
How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers