- Home /
Could someone write me a 'zoom' script?
Could someone to write me a script to make a gun zoom in when you hold down Z then when you release Z it zooms back out.What I need is a machine gun zoom,the kind on halo.
Plus, I need to know what kind of script I would write it in and what object the script would be in.
I would really appreciate any help I can get,Thanks.
Please read the FAQ. Asking people to write scripts for you is not the purpose of this site.
Answer by DaveA · Apr 12, 2011 at 03:32 AM
You want something like this:
function Update()
{
if (Input.GetKeyDown("z"))
Camera.main.fieldOfView = 10;
if (Input.GetKeyUp("z"))
Camera.main.fieldOfView = 90;
}
If you want that zoom effect to interpolate over, say, a littel over a second, that's also easy
var fov = 90;
function Update()
{
if (Input.GetKeyDown("z"))
fov = 10;
if (Input.GetKeyUp("z"))
fov = 90;
if (Camera.main.fieldOfView < fov)
Camera.main.fieldOfView++;
if (Camera.main.fieldOfView > fov)
Camera.main.fieldOfView--;
}
There's ways to adjust the speed, let me know if you need that.
In general, doing small fixed changes (like ++ / --) is not portable, since it varies with framerate. A simple portable pattern is to add/subtract a multiple of Time.deltaTime.
He said,"There's ways to adjust the speed, let me know if you need that." :) It was nice of him to spoonfeed so much code as it is.
You can click on the tick beside his answer to mark that it is the answer you are looking for, and you can just comment on his answer to thank him, not use the answer your own question feature to thank him. =)
For all you jerks saying that im being spoonfed, just shut up!Im new to scripts I have no idea how they work. Im only 12 years old, and if I don't ask these kind of questions I'll never learn anything.
And I don't really care what this forum is for. I tried to write the script myself but I couldn't, so I asked for some help. Im just glad one of you had the decency to acually write something useful, which is the point of having a forum anyway!
Thank you DaveA. Im glad you understand the differance between help and insults.
This isn't a forum. As I said, read the FAQ. This is a Q&A database for specific questions; polluting it with off-topic stuff makes it worse for everybody. Learning to script is good, but don't ask people to write scripts for you here. If you want to use a forum, go to a forum, such as http://forum.unity3d.com.
Answer by G54 · Apr 12, 2011 at 03:06 AM
Not a spoon feed community, but if you ask more of a specific question people are more than willing to help. Ex for when you press z if(Input.GetKeyDown("z") { //zoom camera } You need to make a nested if statment if you know what that means.
-Mike G.
Answer by AhmadYusaf · Jul 13, 2021 at 06:52 AM
Hey you can use the following script if you want to zoom in 2d . this is pinch to zoom.
float previousDistance; float zoomSpeed = 8.5f;
float minZoomOut = 1;
float maxZoomOut = 15;
GameObject selectedObject;
void Update()
{
if (Input.touchCount == 2 && (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began))
{
// calibrate the distance value;
previousDistance = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);
}
else if (Input.touchCount == 2 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
{
float distance;
Vector2 touch1 = Input.GetTouch(0).position;
Vector2 touch2 = Input.GetTouch(1).position;
distance = Vector2.Distance(touch1, touch2);
//camera based on pinch/zoom
float PinchAmount = (previousDistance - distance) * zoomSpeed * Time.deltaTime;
if (Camera.main.orthographic)
{
//Debug.Log("PinchAmount Val is "+ PinchAmount);
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize + (PinchAmount * 0.05f), minZoomOut, maxZoomOut);
}
else
Camera.main.transform.Translate(0, 0, PinchAmount);
previousDistance = distance;
}
}