GUI button for camera rotation

Hi everyone - new to Unity and Javascript, so sorry in advance for my density. I’m sure I’ve seen the answer to my question several times, but I’m just not putting it together. I have a script for “rotate camera” which I pulled from the shadow demo. It works exactly the way I want, except that instead of using the keyboard arrow keys to rotate, I want to use buttons on a GUI. Here’s the rotate camera script:

var target : Transform;
var edgeBorder = 0.1;
var horizontalSpeed = 360.0;
var verticalSpeed = 120.0;
var minVertical = 20.0;
var maxVertical = 85.0;

private var x = 0.0;
private var y = 0.0;
private var distance = 0.0;

function Start()
{
	x = transform.eulerAngles.y;
    y = transform.eulerAngles.x;
    distance = (transform.position - target.position).magnitude;
}

function LateUpdate()
{
	var dt = Time.deltaTime;
	x -= Input.GetAxis("Horizontal") * horizontalSpeed * dt;
	y += Input.GetAxis("Vertical") * verticalSpeed * dt;
	
	y = ClampAngle(y, minVertical, maxVertical);
	 
	var rotation = Quaternion.Euler(y, x, 0);
	var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
	
	transform.rotation = rotation;
	transform.position = position;
}

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);
}

And, I’ve even made GUI buttons that work great for the zoom in/out function I want, that goes like this:

	if(GUI.RepeatButton(Rect(Screen.width/2 -19 , Screen.height -50, 18, 18), "+")){
	camera.main.fieldOfView = camera.main.fieldOfView - .5;
	}
	
	if(GUI.RepeatButton(Rect(Screen.width/2 +1 , Screen.height -50, 18, 18), "-")){
	camera.main.fieldOfView = camera.main.fieldOfView +.5;
	}

So, now it seems all I have to do is combine them somehow to use GUI keys to do what the keyboard arrow buttons are doing in the rotate script, but I can’t figure out how to make it work. Thanks for any hints you can provide - I will take no offense if you need to talk down to me with details (“Type this HERE and attatch that HERE”), I’m sure I’ve seen the answer in here somewhere, but things like “you just need to set up a variable and pass it to the function” tend to mystify me more. Thanks again.

The main difference is that you need to put all GUI code (or at least the calls to GUI.xxx functions) in the OnGUI function:-

function OnGUI() {
    if (GUI.RepeatButton(...)) {
        // Camera code
    } 
}

The only other slight difference is that Input.GetAxis returns a value from -1 to +1 depending on which direction is input. Obviously, you need to do this explicitly with your buttons, so you’ll have something like:-

if (GUI.RepeatButton(...)) {    // Left button.
    x -= horizontalSpeed * dt;
}  else if (GUI.RepeatButton(...)) {    // Right button.
    x += horizontalSpeed * dt;
}
1 Like

Thanks so much Andeeee – this helps a lot. Appreciate your time!

NZ

Hey NoobZilla~

I am trying to achieve something similar to this, I was wondering if I could possible see your completed script or scene? I am quite noob with scripting but am able to understand the outline :wink:

Thanks in advance

in love with your brain “Andeeee”