Invisible buttons controls / Area control

I’m new for unity and also english is not my native language so please excuse me for my eventual grammar mistakes …

33992-untitled.png

In the photo i have 4 regions . I want to create those buttons to make the main object to move up/top/left/right . But also I want this buttons to be invisible . I don’t know how to solve this problem . I’m lurking around google for 2 hours and i can’t find a solution/tutorial for this . I want to create a game which manly is based on speed and accuracy and the joystick controller / classic buttons doesn’t help him to much .

// i forgot to mention that my game is a top-down 2d game …

1 Answer

1

Hello,

i dont realy see where your mainobject is, but i will try to help you without knowing it.

when you want to controll an object via buttons that it walks, you have to attach a script to the
mainobject. this object should own the knowledge of the buttons or methods who are connected with the listeners of the buttons.

to be able to move the object.
below i show you an example code

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ButtonScript : MonoBehaviour {

	public enum ORIENTATION { LEFT, TOP, RIGHT, BOTTOM };
	public ORIENTATION buttonOrientation;

	// Use this for initialization
	void Awake() {
		this.GetComponent<Button>().onClick.AddListener(moveObject);
	}

	private void moveObject(){
		GameObject.Find("MainCharacter").GetComponent<MovementScript>().moveCharacter(buttonOrientation);
	}
}

using UnityEngine;
using System.Collections;

public class MovementScript : MonoBehaviour {
	/**
	 * Method to move the character for one cell
	 **/
	public void moveCharacter(ButtonScript.ORIENTATION _orientation){
		Vector2 position = this.transform.position;
		switch(_orientation){
		case ButtonScript.ORIENTATION.LEFT: 
			position.x -= 10;
				break;
		case ButtonScript.ORIENTATION.TOP:
			position.y += 10;
				break;
		case ButtonScript.ORIENTATION.RIGHT:
			position.x += 10;
				break;
		case ButtonScript.ORIENTATION.BOTTOM:
			position.y -= 10;
				break;
		}
		this.transform.position = position;
	}
}

with this code you can move your character via the buttons. here is a link to the testproject i created for you. you can use the sourcecode if you want. there are better ways todo this, but i would say this way is also a clean one :slight_smile:

http://www42.zippyshare.com/v/3341418/file.html

Greetings
Gardosen

Thank you for your answer but I have a little problem ... The project you sent doesn't work. When i try to open it gives me an error ...