- Home /
left and right buttons for android 2d game
i used this code to allow my player to move left and right but i don't know how to include the codes for buttons...i'm just a beginner at this...i would like to credit "gucio devs" since the code i got was from his tutorial...thanks please reply asap :)
using UnityEngine;
using System.Collections;
public class Touch : MonoBehaviour {
public float speed=5f;
public bool grounded;
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = gameObject.GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
}
void FixedUpdate()
{
float h = Input.GetTouch ("Horizontal");
rb2d.AddForce ((Vector2.right * speed) * h);
}
}
Answer by Graham-Dunnett · Jun 18, 2015 at 06:49 PM
So, read the docs:
http://docs.unity3d.com/ScriptReference/Input.GetTouch.html
You'll see that this API is intended for mobile devices. If you then look at:
http://docs.unity3d.com/ScriptReference/Input.html
you'll see advice about how to manage input on PCs. Some of the APIs that you need on laptops/desktops have code examples that should get you sorted.
Your answer
Follow this Question
Related Questions
Touch input broken with multi-touch 1 Answer
Saving coins 2 Answers
Unity + Othello 2D VS AndEngine 0 Answers
Android touches called the Input.GetAxis('Mouse X') 0 Answers