- Home /
c# GUI button controls
So I'm trying to convert my tile-based game into a mobile game, and I need to change the controls from keyboard keys to GUI buttons. I have my player moving in 4 directions: up, down, left and right. I need each of these directions to have it's own buttons. I've watched a lot a of videos and look at lots of other answers, but I can't figure out how to do it. I know that I need to use the OnGUI function, and I've tried, but it says that I cant use "&&."
Here is my current player script for the keyboard, but how would I integrate the OnGUI function into this to have touch controls?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
Vector3 pos; // For movement
float speed = 2.0f; // Speed of movement
static bool[] moving;
void Start () {
moving = new bool[5];
pos = transform.position; // Take the initial position
GetComponent<BoxCollider2D>().enabled = false;
}
void FixedUpdate () {
if (gameObject.tag == "Player") {
moving[4] = !(Vector2.Distance(transform.position, pos) < 0.01f);
}
else {
moving[GetComponent<CollectFruit>().ID] = !(Vector2.Distance(transform.position, pos) < 0.01f);
}
bool doneMoving = DoneMoving ();
if (doneMoving) {
GetComponent<BoxCollider2D>().enabled = true;
}
if(Input.GetKey(KeyCode.A) && doneMoving && transform.position.x > -1.9f) { // Left
pos += Vector3.left;
}
if(Input.GetKey(KeyCode.D) && doneMoving && transform.position.x < 1.9f) { // Right
pos += Vector3.right;
}
if(Input.GetKey(KeyCode.W) && doneMoving && transform.position.y < 1.9f) { // Up
pos += Vector3.up;
}
if(Input.GetKey(KeyCode.S) && doneMoving && transform.position.y > -1.9f) { // Down
pos += Vector3.down;
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed); // Move there
}
public bool DoneMoving() {
return !moving[0] && !moving[1] && !moving[2] && !moving[3] && !moving[4];
}
}
Where do you get this error ? What have you tried in OnGUI(), can you please attach it as well. Also, have you looked at new GUI features (https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/the-new-ui) other than OnGUI
implementation ?
Answer by abraham5361 · Aug 18, 2015 at 04:45 PM
Is your problem texture is not appearing? If so then follow me. Add the following codes Note: it does not anything currently then viewing the texture
//put this in Public class
public Texture Buttonup;
public Texture Buttondown;
public Texture Buttonright;
public Texture Buttonleft;
//you cannot change the variable as much as you want as long you change it in GUI.drawtexture
void onGUI () {
GUI.DrawTexture(new Rect( 5, 5, 50, 50), Buttonup, ScaleMode, ScaleToFit, 1.0F);
GUI.DrawTexture(new Rect(15, 15, 50, 50), Buttondown, ScaleMode, ScaleToFit, 1.0F);
GUI.DrawTexture(new Rect(15, 15, 50, 50), Buttonleft, ScaleMode, ScaleToFit, 1.0F);
GUI.DrawTexture(new Rect(25, 25, 50, 50), Buttonright, ScaleMode, ScaleToFit, 1.0F);
//Please read under the code to know how to use the code
}
First to change size of textures in new Rect(10, 10, SIZE1, SIZE2) To change position of texture new Rect(POSTION1, POSTION2, 50, 50) After new Rect(1, 1, 1, 1) (here comes name of texture change if you change the variables on top of OnGUI
To add a texture first make a picture using Photoshop or other image editing programs or go search ones in google images. Put it inside your game files and go to unity editor,click on your script and in the inspector select your Images by clicking each one
If you have any other questions ask me
And your welcome
The problem is that I can't get the player to move anymore using pos += Vector3.direction, I end up having to use transform.Translate, and that ends up being really glitchy.
Answer by Positive7 · Aug 18, 2015 at 02:04 PM
Somrthing like this and position GUIButtons for your needs :
void OnGUI(){
if (gameObject.tag == "Player") {
moving[4] = !(Vector2.Distance(transform.position, pos) < 0.01f);
}
else {
moving[GetComponent<CollectFruit>().ID] = !(Vector2.Distance(transform.position, pos) < 0.01f);
}
bool doneMoving = DoneMoving ();
if(GUILayout.Button("←") && doneMoving && transform.position.x > -1.9f) { // Left
pos += Vector3.left;
}
if(GUILayout.Button("→") && doneMoving && transform.position.x < 1.9f) { // Right
pos += Vector3.right;
}
if(GUILayout.Button("↑") && doneMoving && transform.position.y < 1.9f) { // Up
pos += Vector3.up;
}
if(GUILayout.Button("↓") && doneMoving && transform.position.y > -1.9f) { // Down
pos += Vector3.down;
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed); // Move there
}
Ok, I'm not sure how I'm going to explain this. In my game, I have the player, and in each direction around him, I have 4 other AI. What is happening is only one AI will move, and then that's it. Otherwise its working :).
Also, how would I position the buttons if it's GUI.Layout?
All 5 characters are supposed to move as one, but when I clicked the down button, only slime4 moved.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[HELP] C# GUI, not showing up in camera 0 Answers
How to hide/show GUI Buttons through another GUI Button? 2 Answers