- Home /
Unity 2D - Basic Button Movement
I already made moving object with arrows but now I want to make arrows on the screen and make player move when arrow is clicked.
So I started with this:
void Update() {
if (Input.GetMouseButtonDown(0))
Debug.Log("Pressed left click.");
}
So the problem is I can't use same script as for arrows and here is why:
void FixedUpdate () {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs(move));
GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
In this code there is no declaration of any buttons clicked and I don't understand anything. Can someone please write script for basic movement of object?
Answer by Firedan1176 · Aug 04, 2015 at 09:53 PM
If you're using Unity 4.6 or greater, you can create your arrow buttons on screen, and just position how you want. These button have a script called Button. At the bottom, there is a field called OnClick. Whenever you press your button in-game, this OnClick field will call whatever functions or variables you place into it, and you can do multiple! For example, if you have a public function called SendMessage() in a script on an empty gameObject in your scene, you can drag your empty gameObject into that OnClick field, select your script, and find the function 'SendMessage()' in the list, you can call that function when you press the button.
Now let's implement this:
Create a script for your buttons. Add this into your script:
public void UseArrowButton(string button) {
if(button == "w")
//Move forward
transform.position += transform.forward * Time.deltaTime;
if(button == "a")
transform.position += -transform.right * Time.deltaTime;
if(button == "s")
transform.position += -transform.forward * Time.deltaTime;
if(button == "d")
transform.position += transform.right * Time.deltaTime;
}
Now it's untested, but you should get the idea. Now place your script on an object on your scene. You could put it on the Camera if you wanted, it doesn't really matter. Now on your buttons, add the Camera object in the OnClick field, navigate to your script, and then the function. Now there should be an empty field to the side where you can enter in a string (which is the 'button'). For each button, put in w, a, s, and d. Now if you want to use Input.GetAxis, you can just replace those.
The OnClick confused me at first, but think of it as when Unity calls Start or Update. That's all it is really doing. When you press the button, the OnClick will call a function on a script in a scene. Just make sure the function has 'public' before it.
But I'm asking for 2D and so for the script of moving in 2D
Yeah you'll need to change it, but you should get the idea.
I'm sure the point of your question is to get buttons to work with movement on the screen. Hopefully, you should be able to change the movement in my example on your own.
Hello .. Thank u so much for your script, but my player object is moving very SLOWLY So please tell me how can i speed up the movements
Hey, sorry for the late response. You can multiply the transform.forward * Time.deltaTime
with another value, say 5, to make it go faster.