- Home /
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
}
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();
}
}
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);
}
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...
sorry, must have been a user error. it didn't seem to work when i was doing it. Thanks for the help