Search Unity

Spacing out GUI Buttons

Discussion in 'Scripting' started by kearley01, Sep 1, 2015.

  1. kearley01

    kearley01

    Joined:
    Sep 1, 2015
    Posts:
    1
    Hi everyone,

    Very new to unity and Scripting. I found this script online for creating buttons to rotate the camera around an object in my scene. The buttons work fine, but I want to space them out across the screen, rather than have them right next to each other, i.e. the UP button at the top, the LEFT button on the left hand side of the screen, etc. It's very simple, I know, but can't figure it out. Any ideas?

    Thanks in advance!

    Here is the script:

    //WASDtoorbit, leftCtrl/Alttozoom
    usingUnityEngine;
    usingSystem.Collections;

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

    publicclassGuikeys : MonoBehaviour {
    publicTransformtarget;
    publicfloatdistance = 20.0f;
    publicfloatzoomSpd = 2.0f;

    publicfloatxSpeed = 240.0f;
    publicfloatySpeed = 123.0f;

    publicintyMinLimit = -723;
    publicintyMaxLimit = 877;

    privatefloatx = 0.0f;
    privatefloaty = 0.0f;

    privatefloatGUIHorizontal = 0;
    privatefloatGUIVertical = 0;

    publicvoidStart () {
    Vector3angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

    //Maketherigidbodynotchangerotation
    if (rigidbody)
    rigidbody.freezeRotation = true;
    }



    voidOnGUI()
    {
    if(Event.current.type == EventType.Repaint)
    {
    GUIHorizontal = 0;
    GUIVertical = 0;
    }
    GUILayout.BeginArea (newRect (10, 10, 100, 100));
    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();
    GUILayout.EndArea ();
    }


    publicvoidLateUpdate () {
    if (target) {
    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;

    Quaternionrotation = Quaternion.Euler(y, x, 0.0f);
    Vector3position = rotation * newVector3(0.0f, 0.0f, -distance) + target.position;

    transform.rotation = rotation;
    transform.position = position;
    }
    }

    publicstaticfloatClampAngle (floatangle, floatmin, floatmax) {
    if (angle < -360.0f)
    angle += 360.0f;
    if (angle > 360.0f)
    angle -= 360.0f;
    returnMathf.Clamp (angle, min, max);


    }
    }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Totally easier with the new UI. There are some tutorials in my YouTube channel that might help. Link is in my signature.

    In particular check out the two on the drop down menu.