Unity 2d make four directions (Up, Down, Right, Left. like RPG games) GUI buttons. Please help me.
First of all, my English ability just a little. I made character and character movement. (Also made Animation, Animator. Four directions) I made to move the keyboard,
Up - keyboard "W" Down - keyboard "S" Right - keyboard "D" Left - keyboard "A"
made in this format(this is a PlayerControllers Script). Format is created as a keyboard works well. But, How to create a GUI button, I do not know what to add, what to PlayerControllers Script. I found roughly, there is a movie that uses GetAxisRaw and Rigidbody2D... I do not know at all. Here are attached to PlayerControllers Script.
using UnityEngine;
using System.Collections;
public class PlayerControllers: MonoBehaviour {
public Animator anim;
public float speed;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.D))
{
transform.Translate(Vector2.right * speed);
}
if (Input.GetKey (KeyCode.A))
{
transform.Translate(-Vector2.right * speed);
}
if (Input.GetKey (KeyCode.W))
{
transform.Translate(Vector2.up * speed);
}
if (Input.GetKey (KeyCode.S))
{
transform.Translate(-Vector2.up * speed);
}
if (Input.GetKey (KeyCode.A)) {
anim.SetBool ("Left", true);
anim.SetBool ("Right", false);
anim.SetBool ("Up", false);
anim.SetBool ("Down", false);
}
if (Input.GetKey (KeyCode.S)) {
anim.SetBool ("Down", true);
anim.SetBool ("Up", false);
anim.SetBool ("Left", false);
anim.SetBool ("Right", false);
}
if (Input.GetKey (KeyCode.D)) {
anim.SetBool ("Right", true);
anim.SetBool ("Left", false);
anim.SetBool ("Up", false);
anim.SetBool ("Down", false);
}
if (Input.GetKey (KeyCode.W)) {
anim.SetBool ("Up", true);
anim.SetBool ("Down", false);
anim.SetBool ("Left", false);
anim.SetBool ("Right", false);
}
if(Input.GetKey (KeyCode.A)){
anim.SetBool("Player_Left", true);
}
else{
anim.SetBool ("Player_Left", false);
}
if(Input.GetKey (KeyCode.S)){
anim.SetBool("Player_Down", true);
}
else{
anim.SetBool ("Player_Down", false);
}
if(Input.GetKey (KeyCode.D)){
anim.SetBool("Player_Right", true);
}
else{
anim.SetBool ("Player_Right", false);
}
if(Input.GetKey (KeyCode.W)){
anim.SetBool("Player_Up", true);
}
else{
anim.SetBool ("Player_Up", false);
}
}
}
I would want to ask. To create a GUI button of the four directions, and I want to move character to four directions (Up, Down, Right, Left), but it doesn't work. I do not know what to do in PlayerControllers Script added. Also attach a Game Scene.
Please help me.
Thank you.
Answer by Xephex · Sep 21, 2015 at 10:58 AM
If you go to Create > UI > Button
You can change the OnClick() section on the right to run any function in your code. For example, if you make a function called ChangeDirectionOfCharacter (or whatever you want to call it), set that function to the OnClick event then make a function something like this:
function ChangeDirectionOfCharacter(dir : String){
if(dir=="up"){
Transform.translate(Vector2.Up * speed);
}
//continue other "if statements" for other directions.
}
You need to change the argument in the editor too. It will look like this (or similar): http://i.imgur.com/dDX9XG0.png
Have you Create > UI > Button is not rather the Create > UI > Image? Button is something that seems strange.
And What is the dir? direction? What Is fits in dir place? Thanks bit $$anonymous$$ore specifically described. please help me...
Your answer
Follow this Question
Related Questions
Click on object shows menu, but also move it. 0 Answers
How do I move a game object only on the x axis with arrow keys? 1 Answer
Moving moving Object with left/right arrow keys in a circular direction 3 Answers
Why the loop in waypoints in not working when set to true? 0 Answers
How do I make objects move with the moving platform? 1 Answer