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 jack_sparrow · Aug 30, 2012 at 05:32 AM · cameraguizoomguibutton

camera zoom using gui button

hi, everyone

i am having little prob while zoomin camera using GUIbutton.

can somebody help me ??

here's code for my camera


 //WASD to orbit, left Ctrl/Alt to zoom
 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Camera-Control/Keyboard Orbit")]
 
 public class Guikeys : MonoBehaviour {
     public Transform target;
     public float distance = 20.0f;
     public float zoomSpd = 2.0f;
 
     public float xSpeed = 240.0f;
     public float ySpeed = 123.0f;
 
     public int yMinLimit = -723;
     public int yMaxLimit = 877;
 
     private float x = 0.0f;
     private float y = 0.0f;
     
     private float GUIHorizontal = 0;
     private float GUIVertical = 0;
     
     public GUIStyle uptexture;
     public GUIStyle lefttexture;
     public GUIStyle righttexture;
     public GUIStyle downtexture;
     public GUIStyle Zoomintexture;
     public GUIStyle Zoomouttexture;
     public GUIStyle reset;
     
     public void Start () {
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
 
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 
     
     
 void OnGUI()
 {
     
         
     
         
     if(Event.current.type == EventType.Repaint)
     {
         GUIHorizontal = 0;
         GUIVertical = 0;
     }
     
      //GUI.Box(new Rect(910,45,106,120),"Camera Control");
     
          //GUI.Box(new Rect(810,175,205,40),"");
     
     GUILayout.BeginVertical();
         
     if(GUI.RepeatButton(new Rect(1130,470,30,30),"",uptexture))
     {
         GUIVertical = 1;
     }
     GUILayout.BeginHorizontal();
     if(GUI.RepeatButton(new Rect(1100,500, 30, 30),"", lefttexture))
     {
         GUIHorizontal = -1;
     }
     if(GUI.RepeatButton(new Rect(1160,500, 30, 30),"", righttexture))
     {
         GUIHorizontal = 1;
     }
     GUILayout.EndHorizontal();
     if(GUI.RepeatButton(new Rect(1130,530, 30, 30),"", downtexture))
     {
         GUIVertical = -1;
     }
     GUILayout.EndVertical();
     
         if(GUI.RepeatButton(new Rect(1130,565, 30, 30),"",Zoomintexture))
     {
 // i was just trying to press keyboard key via GUIbutton but didn't work. 
         //Input.GetKeyDown("Plus");
         
     }
         if(GUI.RepeatButton(new Rect(1130,600, 30, 30),"",Zoomouttexture))
     {
         //Input.GetKeyDown("Minus");
 // same here
             
     }
         if(GUI.RepeatButton(new Rect(1130,50, 30, 30),"",reset))
     {
         //reset camera;
             
     }
     
 }
 
 
     public void LateUpdate () {
         if (target) {
             
             
             x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
             y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
             
             
             x -= GUIHorizontal * xSpeed * 0.02f;
             y += GUIVertical * ySpeed * 0.02f;
             
             y = ClampAngle(y, yMinLimit, yMaxLimit);
             
         distance -= Input.GetAxis("Fire1") *zoomSpd* 0.02f;
             distance += Input.GetAxis("Fire2") *zoomSpd* 0.02f;
             
             Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
             Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
             
             transform.rotation = rotation;
             transform.position = position;
         }
     }
 
     public static float ClampAngle (float angle, float min, float max) {
         if (angle < -360.0f)
             angle += 360.0f;
         if (angle > 360.0f)
             angle -= 360.0f;
         return Mathf.Clamp (angle, min, max);
         
     
     }
 }
Comment
Add comment · Show 3
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 jack_sparrow · Aug 31, 2012 at 04:54 AM 0
Share

no answer ..????

is that really hard question for u guys .. ??

just want some help .

just need direction how do i do it.. plz help

avatar image syclamoth · Aug 31, 2012 at 04:56 AM 1
Share

The problem here is that you haven't asked an answerable question- you've made a statement and posted an entire script that nobody can be bothered reading all the way through to debug. Please ask a specific question, and I'm sure somebody will be able to help you.

avatar image jack_sparrow · Aug 31, 2012 at 05:19 AM 0
Share

ohhh thanx Syclamoth

but i put the entire script bcoz i want exact solution.

to understand exactly what m i asking.

anyways

can u help me .. i just want to add zoom function in button.

i can do it with key board and mouse scroll but i also want to add in gui button.

thanx

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kristercollin · Aug 31, 2012 at 05:55 AM

Jack,

When you say "i can do it with key board and mouse scroll but i also want to add in gui button.", do you mean that you had a function to scroll the camera while the game was playing when a certain key was pressed?

If that is what you meant, then you probably would have had something like the following, right?

 if (Input.GetKeyDown("q"))
 {
   ZoomIn();
 }

If that worked, then all you have to do is put the function call as a response to your GUI Button being pressed, like such:

 if(GUI.RepeatButton(new Rect(1130,565, 30, 30),"",Zoomintexture))
 {
   ZoomIn();
 }

Hope that helps.

As a final point, though, if you had spent just a bit of time googling and thinking about the problem, you would likely have solved it yourself, as it's a question that's been asked before (many times, a quick google search tells me). If you want to guarantee that you will have our help, then just show us that you've tried doing the learning yourself, show us that you tried some solutions out yourself and that you had enough initiative to look and see if the question was already answered before asking it.

Comment
Add comment · Show 1 · 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 jack_sparrow · Aug 31, 2012 at 06:21 AM 0
Share

thanx for replay kristercollin

actually i tried to find solution but i didn't get it.

i don't have any zoo$$anonymous$$ function. even i didn't have any input.Get$$anonymous$$eyDown.

u can check in my script

i used distance variable for which calculate distance between camera and target and it will controll by CTRL and ALT (Fire1 and Fire2 )

i want to execute fire1 and fire2 on gui button down.

thanx

avatar image
0

Answer by Griffo · Aug 31, 2012 at 07:22 AM

Do you know how to add a GUI button with touch input? If you do add this to the script and call it when touch.. Not tested ..

 private var toggle : boolean = true;        // Is the camera zoomed in
 private var time : float;
 
 var zoom : int = 20;
 var normal : int = 60;
 var smooth : float = 5;
 
 function Awake() {
 
 }
 
 function Start () {
 
 }
 
 function Update () {

 // Your touch input here and call zoomCamera function
 
 }
 
 function zoomCamera(){
 
 time = smooth;
 
     if(toggle){
         while (time > 0.0f){
             Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView,zoom,Time.deltaTime*smooth);
             time -= Time.deltaTime;
             yield;
         }
             toggle=false;
 
         }else{
  
          while (time > 0.0f){
             Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView,normal,Time.deltaTime*smooth);
             time -= Time.deltaTime;
             yield;
         }
             toggle=true;
     }         
 }
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

10 People are following this question.

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

Related Questions

Raycast misses after camera zoom 2 Answers

flickering lines when zooming fov (both GUI and ortho cameras). 2 Answers

Unity2D - Camera in exported game is zoomed out compared to unity game preview HELP PLEASE! 2 Answers

Moving camera around with GUI buttons 4 Answers

Is there a way to zoom GUI and scene at the same time? 0 Answers


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