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 PwnedByACheeseburger · Dec 14, 2010 at 01:44 AM · animationcamerafpsgun

Zooming in with a gun, Halo style.

Hello,

I am trying to make a game where you can press and hold a button and you will be zoomed in like in halo. So far, I have this:

function Update () {
if (Input.GetButtonDown("Fire2"))
{
    camera.fieldOfView = 26;
}
else if (Input.GetButtonUp("Fire2"))
{
    camera.fieldOfView = 66;
}

}

With this code, the camera just jumps forward. I want it to smoothly zoom in and out. Let me know if I can make myself clearer.

Thanks

Tate

Comment
Add comment
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

2 Replies

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

Answer by Helix · Dec 14, 2010 at 02:53 AM

Ahoy there, Tate.

Here's a quick and dirty solution. Put this in at the beginning of your script:

private var zoom : boolean = false;

and change your current code to:

var zoomSpeed : float = 2.5;

if (Input.GetButtonDown("Fire2")) { if(zoom==false){zoom=true;} else{zoom=false;} }

if(zoom==false) { if(camera.fieldOfView<66) { camera.fieldOfView = Mathf.Clamp(camera.fieldOfView+zoomSpeed,26,66); } } else { if(camera.fieldOfView>26) { camera.fieldOfView = Mathf.Clamp(camera.fieldOfView-zoomSpeed,26,66); } }

Hope that's what you're after

Comment
Add comment · Show 4 · 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 PwnedByACheeseburger · Dec 14, 2010 at 09:05 PM 0
Share

Thanks very much! One more thing, the zoom in is pretty slow. Is there a way to make it zoom in quickly?

avatar image Helix · Dec 15, 2010 at 01:35 AM 0
Share

Sure! I edited the code. The ++ and -- always add and subtract by 1, they gave you a fixed "zoom speed" of 1. Now you can set the zoomSpeed to whatever you like. So long as I haven't made any typos, that should work fine.

avatar image Statement · Dec 15, 2010 at 02:43 AM 0
Share

You are missing Time.deltaTime so it is frame-rate dependent which probably isn't what the OP wanted.

avatar image PwnedByACheeseburger · Dec 15, 2010 at 08:24 PM 0
Share

Thanks you both! I ended up combining the two scripts and have exactly what I was looking for.

avatar image
4

Answer by Statement · Dec 15, 2010 at 02:35 AM

Here's a configurable scope script.

You can adjust the following variables directly on the inspector, but it should work out of the box for you. It requires a camera.

  • Scope Button
  • Scope Speed
  • Scope Fov
  • Normal Fov


var scopeButton : String = "Fire2"; var scopeSpeed : float = 250; var scopedFov : float = 26; var normalFov : float = 66;

camera.fieldOfView = normalFov;

function Update() { var isScoping = Input.GetButton(scopeButton); var targetFov = isScoping ? scopedFov : normalFov;
var speed = scopeSpeed * Time.deltaTime; camera.fieldOfView = Mathf.MoveTowards(camera.fieldOfView, targetFov, speed); }

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

No one has followed this question yet.

Related Questions

FPS Aim Down Sights. Exact Position. 1 Answer

animating multiple weapons with 2 arms 0 Answers

Animations have been cleared, help! 0 Answers

FPS gun movement 1 Answer

Camera changing with animation. 0 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