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
0
Question by Cocotimba · Jan 02, 2012 at 01:21 AM · inputgetkeydowngetkeygetkeyup

Change view if key is pressed

I am trying to change Camera view if user presses down a key.

 if (Input.GetKeyDown("z"))
 {
   // Change Camera fov to 20.
 }
 else if (Input.GetKeyUp("z"))
 {
   // Change Camera fov to 10.
 }

The problem is that makes me hold down the z on the keyboard and once I let it go I have to go the "else if (Input.GetKeyUp("z"))" takes effect.

What I want is for the user to press z and until he presses it again the Camera stays with fov to 20.

How would I do this, I tried get key and it didnt work; almost like both if and else executed at the same time due to how fast the update method is being called.

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 djfunkey · Jun 29, 2012 at 06:29 AM 0
Share

Hi try this C# script i made, put this on your camera

 //set this to your defualt / standard fov
 private float FieldOfView = 80;
 
 // Update is called once per frame
 void Update () {
     //when Z hey is pressed
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z))
     {
         //change the fov to 20
         gameObject.transform.camera.fieldOfView = 20;
     }
     //when Z key is released 
     if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Z))
     {
         //change the fov to 80
         //(set the fov number here, to match the fov number in the private float)
         gameObject.transform.camera.fieldOfView = 80;
     }
 }

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Grady · Jan 02, 2012 at 01:41 AM

Hey,

Using Input.GetKey() should work.....

Try this script:

function Update(){
    if(Input.GetKey("z")){
        //Change camera fov to 20
    }
}

I hope I fully understood what you were trying to do, which was change the fov to something while the z key is held down, and then change it back/to something else, once the z key is released.....

Hope this helps you!!!!!!

Comment back if you need more help!!!!!!

-Grady

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 Cocotimba · Jan 02, 2012 at 01:53 AM

Hi Grady, thanks for your post. Your code doesn't work, because if GetKey is not "z" then the else will always execute.

I tried different variations of the else, such as

 function Update()
 {
    if(Input.GetKey("z"))
    {
     //Change camera fov to 20
     }
     else if (Input.GetKeyDown("z"))  
     {      
      //Change camera fov to 10
     }
 }

That doesn't work either. Any ideas?

Comment
Add comment · Show 2 · 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 Grady · Jan 02, 2012 at 02:06 AM 0
Share

I updated my answer, so that it is just the same thing without the else, that might work....!!! Also, remember when you are going to commment back, to use the comment box underneath the answer!!!!!!!!!!!!!!!!!!! :D

avatar image Grady · Jan 02, 2012 at 11:58 AM 0
Share

You could maybe try your code just above, but chnageGet$$anonymous$$eyDown to Get$$anonymous$$eyUp.......!!!

avatar image
0
Wiki

Answer by djfunkey · Jun 29, 2012 at 07:49 AM

Hi i know the C# script for this


using UnityEngine; using System.Collections;

public class FieldOfViewChange : MonoBehaviour {

 //set this to your defualt / standard fov
 private float FieldOfView = 80;
 
 // Update is called once per frame
 void Update () {
     //when Z hey is pressed
     if (Input.GetKeyDown(KeyCode.Z))
     {
         //change the fov to 20
         gameObject.transform.camera.fieldOfView = 20;
     }
     //when Z key is released 
     if (Input.GetKeyUp(KeyCode.Z))
     {
         //change the fov to 80
         //(set the fov number here, to match the fov number in the private float)
         gameObject.transform.camera.fieldOfView = 80;
     }
 }

}

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 iuripujol · Nov 26, 2013 at 07:02 PM

if got it!!!!!!

please contact me for the code! :)

iuripujol@hotmail.com

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dialogue with GetKey - Codes 1 Answer

how can i use getkey and getkeydown for two diffrent functions. 2 Answers

Play half of Animation on KeyDown/Other Half on KeyUp 1 Answer

Input.GetKey question 1 Answer

Can/how do I set up a boolean to continue an animation after button press 1 Answer


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