- Home /
Triggering Main Camera Script with GUITexture
So on my main camera I have a script to zoom in, and on my GUITexture I have a script that makes it a button
Here is my Zoom.js script,
var zoom : int = 20;
var normal : int = 60;
var smooth : float = 5;
private var isZoomed = false;
function Update () {
if (Input.GetButtonDown ("zoombutton")) {
isZoomed = !isZoomed;
}
if(isZoomed == true){
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView,zoom,Time.deltaTime*smooth);
}
else{
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView,normal,Time.deltaTime*smooth);
}
}
and here is my ZoomButton.js script,
var recoveryTime = 10;
private var delay = 0;
function Update () { if (delay>0){delay -=1;}
if (delay == 0){ if (iPhoneInput.touchCount ==1){
var currentTouch:iPhoneTouch = iPhoneInput.touches[0];
if(currentTouch.phase ==
iPhoneTouchPhase.Began && guiTexture.HitTest(currentTouch.position)){ guiTexture.color = Color(0.2, 0.3, 0.4, 0.5);
}
}
} }
I want to make it so when I press the button it will activate the script on my main camera. I believe the issue is I don't know how to call on the button being pressed to active the zoom function
Appreciate all and any help, Thanks!
you could have a go with using enabled = true and false, enable the zoom script when touched and disable when let go.
or you could use a while statement so in your guy script you will call a separate function in the zoom script and that will have a while guy is pressed.
me personnally i wouldn't have two different scripts, as having more than needed update functions can slow down the game especially on the iPhone, i always try to have one update, running at any time, sometimes its unavoidable, but a lot of other stuff can be done through different functions.
Answer by Seth-Bergman · Jul 25, 2012 at 11:23 PM
just move the line into the touch statement(also combined to single script):
var recoveryTime = 10;
private var delay = 0;
var zoom : int = 20;
var normal : int = 60;
var smooth : float = 5;
private var isZoomed = false;
function Update () {
if (Input.GetButtonDown ("zoombutton")) {
isZoomed = !isZoomed;
}
if(isZoomed == true){
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView,zoom,Time.deltaTime*smooth);
}
else{
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView,normal,Time.deltaTime*smooth);
}
if (delay>0){
delay -=1;
}
if (delay == 0){
if (iPhoneInput.touchCount ==1){
var currentTouch:iPhoneTouch = iPhoneInput.touches[0];
if(currentTouch.phase==iPhoneTouchPhase.Began && guiTexture.HitTest(currentTouch.position)){
guiTexture.color = Color(0.2, 0.3, 0.4, 0.5);
isZoomed = !isZoomed;
}
}
}
}
note: if you don't want to attach this to your camera , it wouldn't be a problem as long as your camera is tagged as "$$anonymous$$ain Camera".. you just change the lines that say
camera.fieldOfView = etc...
to
Camera.main.fieldOfView = etc...
then it doesn't matter what the script is on