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 sam-luk · Jan 22, 2012 at 10:05 PM · guigui-button

Gui Buttons

Hi

I'm trying to create a reset button for my camera, which is called from a different script which looks for the GUI Button being clicked? I've currently got a script called: "screenHUD" and a camera script called "orbit_camera" (both in Java) i need some way for the Orbit_camera script to listen for the ScreenHud button.

 Pusedo code in Orbit_camera script:
 
 if(input == button1InscreenHUDscript)
 {
  doThisFunction
 }
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

2 Replies

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

Answer by Lo0NuhtiK · Jan 22, 2012 at 10:13 PM

Why have it listen for the other one? Why not just have the other one yell at the first when it's doing something?

Example :

//Orbit_Camera script

function MyFunction(){ //do stuff }


//screenHUD script 
var orbitScript : Orbit_CameraScriptName ;
 
function Start(){
   //assuming these scripts are on the same object
   orbitScript = gameObject.GetComponent(Orbit_CameraScriptName) as Orbit_CameraScriptName ;
}
 
function OnGUI(){
   if(GUI.Button(Rect(5,5,50,50),"Whatever")){
       orbitScript.MyFunction();
   }
}
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
avatar image
0

Answer by sam-luk · Jan 23, 2012 at 11:14 AM

It's because the orbit script uses Late update, and the only way i can effect the camera is if i call the function within the Late update: Currently i can press "a" to achieve what i want but i want to allow the user to click on a GUI Button.

//i've commented around the line

 var target : Transform; 
 
 var targetHeight = 2.0; 
 var distance = 25; 
 
 var maxDistance = 80; 
 var minDistance = 10; 
 
 var xSpeed = 250.0; 
 var ySpeed = 120.0; 
 
 var yMinLimit = -150; 
 var yMaxLimit = 100; 
 
 var zoomRate = 20; 
 
 private var x = 0.0; 
 private var y = 0.0; 
 
 private var initialPosition : Vector3;
 private var initialRotation : Quaternion;
 
 @script AddComponentMenu("Camera-Control/MouseOrbitZoom") 
 
 function Start () { 
     var angles = transform.eulerAngles; 
     x = angles.y; 
     y = angles.x; 
 
    initialPosition = transform.position;
    initialRotation = transform.rotation;
 
    // Make the rigid body not change rotation 
       if (rigidbody) 
       rigidbody.freezeRotation = true; 
 } 
 function ResetCamera () {
   transform.position = initialPosition;
   transform.rotation = initialRotation;
 
    var angles = transform.eulerAngles; 
     x = angles.y; 
     y = angles.x; 
 
   print(transform.position.x);
 }
 
 function LateUpdate () 
 { 
    if(!target) 
       return; 
     
    // If either mouse buttons are down, let them govern camera position 
    if (Input.GetMouseButton(0) || Input.GetMouseButton(1)) 
    { 
    x += Input.GetAxis("Mouse X") * xSpeed * 0.02; 
    y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02; 
    }
 
    distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance); 
    distance = Mathf.Clamp(distance, minDistance, maxDistance); 
     
    y = ClampAngle(y, yMinLimit, yMaxLimit); 
     
    var rotation:Quaternion = Quaternion.Euler(y, x, 0); 
    var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0)); 
     
    transform.rotation = rotation; 
    transform.position = position; 

//////////////////////////////////////////////////////////////////// //// I want to use a gui button (in a different script) instead of "a"/////// ///////////////////////////////////////////////////////////////////

    if(Input.GetKey ("a"))
    {
       ResetCamera();
    }
 } 
 
 static function ClampAngle (angle : float, min : float, max : float) { 
    if (angle < -360) 
       angle += 360; 
    if (angle > 360) 
       angle -= 360; 
    return Mathf.Clamp (angle, min, max); 
 }
Comment
Add comment · Show 2 · 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 Lo0NuhtiK · Jan 23, 2012 at 07:55 PM 0
Share

I just tested your code, got rid of the "a-key" press for resetting the camera, and put the gui on another script like I suggested in my answer, and it works just fine... so...

avatar image sam-luk · Jan 25, 2012 at 10:43 AM 0
Share

sorry, must have been a user error. it didn't seem to work when i was doing it. Thanks for the help

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a GUI.Button to Instantiate gameObject using another gameObject's location 1 Answer

Loot GUI, chest, lists and gameobjects Problem. 2 Answers

GUI button off centre 1 Answer

Changing GUI Buttons While Mouse Down 0 Answers

Unity GUI Style with if Statements 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