- Home /
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!
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.
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
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())
.
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.