Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-2
Question by Alex R · Apr 12, 2011 at 12:04 AM · zoom

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Apr 12, 2011 at 12:34 AM 1
Share

Please read the FAQ. Asking people to write scripts for you is not the purpose of this site.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Apr 12, 2011 at 03:50 AM 0
Share

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.

avatar image NewfieJoe · Apr 12, 2011 at 04:10 AM 0
Share

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.

avatar image DaveA · Apr 12, 2011 at 06:19 AM 0
Share

Hence the qualifier "something like this"

avatar image Alex R · Apr 13, 2011 at 12:57 AM 0
Share

Thanks DaveA you helped a lot

avatar image Nolirneen Alex R · Apr 13, 2011 at 05:00 AM 0
Share

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. =)

avatar image Eric5h5 Alex R · Apr 14, 2011 at 02:08 AM 0
Share

Don't post comments as answers please.

avatar image Alex R · Apr 14, 2011 at 12:30 AM 0
Share

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.

avatar image Eric5h5 Alex R · Apr 14, 2011 at 02:07 AM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Mouse Wheel Zoom 7 Answers

zooming in and out 1 Answer

my camera zooms when I hit the arrow keys - not sure how to disable 1 Answer

2D camera zoom in comparison to the height of target 2 Answers

How do I scale just the time portion of the animation view timeline? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges