Question by
sportente · Mar 10, 2016 at 02:18 PM ·
c#guitransform.positionguitexture
Moving GUI Label through key board?
I created a GUI Label in my game through a texture. Now I would like to move this sphere up and down using the keyboard. Is this possible? If yes how? Within the GUI function or in void update? Thank you for your help.
Here my code:
using UnityEngine;
using System.Collections;
public class GUISphere : MonoBehaviour {
public Texture2D sphere;
public int x;
public int y;
public float speed = 1.5f;
// Use this for initialization
void Start () {
}
void OnGUI() {
GUI.Label(new Rect(10,10,sphere.width, sphere.height),sphere);
//if (Input.GetKey (KeyCode.UpArrow))
//{
transform.position += Vector3.up * speed * Time.deltaTime;
//}
}
// Update is called once per frame
void Update () {
}
}
Comment
Answer by Positive7 · Mar 12, 2016 at 12:00 AM
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public float x;
public float y;
public float speed = 10f;
void OnGUI() {
GUI.Box (new Rect(x,y,120, 120),"TestBox");
}
void Update () {
if (Input.GetKey (KeyCode.DownArrow))
{
y += 1 * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.UpArrow))
{
y += -1 * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.RightArrow))
{
x += 1 * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.LeftArrow))
{
x += -1 * speed * Time.deltaTime;
}
}
}