Control the scene camera using arrow gui buttons

Hi everyone

I’m attempting to control my camera arround my game object through keyboard arrows but i need to put arrow buttons on gui so anyone can control the same thing from gui also.

right now i have this script.

//WASD to orbit, left Ctrl/Alt to zoom
using UnityEngine;

[AddComponentMenu("Camera-Control/Keyboard Orbit")]

public class KeyboardOrbit : 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;

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

public void LateUpdate () {
    if (target) {
        x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
        y += Input.GetAxis("Vertical") * 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);
}

here camera orbit is controlled by keyboard.i need it to be controlled by arrow buttons on gui.

THANKS.

First off, add some private variables for communicating between the GUI functions and the movement functions- call them something like GUIHorizontal and GUIVertical.

private float GUIHorizontal = 0;
private float GUIVertical = 0;

Then, wherever you have a call to ‘Input.GetAxis(whatever)’ replace that with the appropriate GUI axis (horizontal or vertical)- for example:

x -= GUIHorizontal * xSpeed * 0.02f;

Now, make a GUI function that sets the horizontal and vertical numbers according to buttons you are currently holding down. Obviously I don’t know where you need to place the buttons on your screen, so I’ll just use GUILayout for the purposes of this tutorial.

void OnGUI()
{
    if(Event.current.type == EventType.Repaint)
    {
        GUIHorizontal = 0;
        GUIVertical = 0;
    }
    GUILayout.BeginVertical();
    if(GUILayout.RepeatButton("UP"))
    {
        GUIVertical = 1;
    }
    GUILayout.BeginHorizontal();
    if(GUILayout.RepeatButton("LEFT"))
    {
        GUIHorizontal = -1;
    }
    if(GUILayout.RepeatButton("RIGHT"))
    {
        GUIHorizontal = 1;
    }
    GUILayout.EndHorizontal();
    if(GUILayout.RepeatButton("DOWN"))
    {
        GUIVertical = -1;
    }
    GUILayout.EndVertical();
}

This will move the camera around according to those four buttons, instead of the keyboard!