x


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!

more ▼

asked Jul 25 '12 at 10:42 PM

freeride474 gravatar image

freeride474
2 2

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.

Jul 25 '12 at 11:15 PM reptilebeats
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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; 
     }
   }
 }   
}
more ▼

answered Jul 25 '12 at 11:23 PM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

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 "Main 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

Jul 25 '12 at 11:26 PM Seth Bergman
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3738
x3003
x2000
x954
x475

asked: Jul 25 '12 at 10:42 PM

Seen: 294 times

Last Updated: Jul 25 '12 at 11:27 PM