Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Adam_Lighthouse · Jul 29, 2015 at 08:35 PM · javascriptguibutton

Button OnClick Applied To Whole Canvas?

Hello,

I've recently started learning Unity and I've come into a bit of a problem that I can't seem to fix.

I'm simply trying to create a UI Button that when clicked will re-focus the camera (Zooming in to a character head).

I've tired attaching the below script to both the button, and to a game object and then using the onClick event to launch it, but whenever I click anywhere in the screen, it zooms in, when I only want it to happen on my Button Click.

Code:

 #pragma strict
 
 var target : Camera;
 var zoom : int = 20;
 var normal : int = 60;
 var smooth : float = 5;
 
 private var isZoomed = false;
 
 
 function Start () {
 
 }
 
 function Update () { 
     if (Input.GetMouseButtonDown(0)){ isZoomed = !isZoomed; }
     {
         {
         //target.main.transform.Translate(1,1,1);
         //target.fieldOfView = 20;
 
             if(isZoomed == true)
             {
             target.fieldOfView = Mathf.Lerp(target.fieldOfView,zoom,Time.deltaTime*smooth);
             }
             
             else
             {
             target.fieldOfView = Mathf.Lerp(target.fieldOfView,normal,Time.deltaTime*smooth);
             }
         
         }  
     }
 }

Any advice would be really helpful.

Many thanks!

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

1 Reply

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

Answer by Dave-Carlile · Jul 29, 2015 at 08:38 PM

Your script is specifically checking for a mouse button press. So it doesn't matter what object you attach it to, it's going to execute the code inside of the if mouse is pressed. It's running this code inside of Update so it's going to run every frame.

Add a method to your script called something like Zoom, and just put your zoom code in it, which is everything after the if. In pseudocode it would look something like this.

 function Zoom()
 {
   zoomed = !zoomed
   if zoomed
     set the zoomed field of view
   else
     set the unzoomed field of view
 }


Completely get rid of the Update code.

Once you have that function, that's what you want to call in the button click event.

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 Adam_Lighthouse · Jul 30, 2015 at 08:08 AM 0
Share

Thanks, that made the script work fine, the only issue I am having now is how the camera zooms in and out, it seems to zoom in a lot more than it does zoom out, which is making the camera move forward every time you 'zoom'. I presume this is something to do with the variables (zoom / normal) but even when i change those it still does the same movement. Do you think there is anything wrong with my script that would make this happen?

(Trying to zoom to FoV 20, reset back to 60 as 'normal')

 #pragma strict
 
 var target : Camera;
 var zoom : int = 20;
 var normal : int = 60;
 var smooth : float = 5;
 
 private var isZoomed = false;
 
 
 function Start () {
 
 }
 
 function update () {
 
 }
 
 function Zoom ()
 {
      isZoomed = !isZoomed;
     if(isZoomed == true)
     {
     target.fieldOfView = $$anonymous$$athf.Lerp(target.fieldOfView,zoom,Time.deltaTime*smooth);
     }
 
     else
     {
     target.fieldOfView = $$anonymous$$athf.Lerp(target.fieldOfView,normal,Time.deltaTime*smooth);
     }
 }

Thanks

avatar image Dave-Carlile · Jul 30, 2015 at 12:00 PM 1
Share

First, Lerp is generally best used with a fixed starting point and fixed ending point, and the final parameter varying from 0 to 1. The final parameter can be thought of as a percentage. You're either 0% away from the start, or 100% away from the start (or at the target).

I would suggest using $$anonymous$$athf.$$anonymous$$oveTowards here ins$$anonymous$$d as it's designed for passing in a current value and target value. You can also specify a max speed value. But this is just a nitpicky thing on my part.

Now, the reason it isn't always moving the same amount is that the Zoom function is only called once, so it moves fieldOfView just deltaTime * smooth units towards the target. Both Lerp and $$anonymous$$oveTowards need to be called multiple times over the course of many frames, each time moving the value a little bit until it reaches the target.

I would suggest reading about Coroutines. They are designed for this exact scenario. Your Zoom function would become a coroutine, and rather than calling it outright when the key is pressed, you would call StartCoroutine(Zoom()).

avatar image Adam_Lighthouse · Jul 30, 2015 at 01:03 PM 0
Share

Thanks for all the help Dave, i'll try this out!

avatar image Adam_Lighthouse · Jul 30, 2015 at 02:23 PM 0
Share

So I have changed my code to:

 #pragma strict
 
 var target : Camera;
 var currStrength: float;
 var maxStrength: float;
 var recoveryRate: float;
 
 var prevStrength: float;
 var ResetPos: float;
 
 
 private var isZoomed = false;
 
 private var flipswitch = false;
 
 function Start () {
 
 }
 
 function update () {
 
     Zoom(target);
 
 }
 
 function Zoom (target : Camera)
 {
     isZoomed = !isZoomed;
         if(flipswitch == true)
         {
         target.fieldOfView = $$anonymous$$athf.$$anonymous$$oveTowards(currStrength, maxStrength, recoveryRate * Time.deltaTime);
         flipswitch = false;
         }
 
         else if (flipswitch == false)
         {
         target.fieldOfView = $$anonymous$$athf.$$anonymous$$oveTowards(prevStrength, ResetPos, recoveryRate * Time.deltaTime);
         flipswitch = true;
         }
         yield;
 }


This all works fine, but there is no animation between the two points. Is this something I have to add into my code (or via the animation screen?), i assumed the update function would handle this for me.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUI.Toggle to only send one action, not repeated 1 Answer

Scripting a GUI button to move object smoothly 1 Answer

Select a game object and perform actions using GUI.Button (EventTrigger) 2 Answers

Make button clickable 2 Answers

Hide/show GUI Buion 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