Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
add Right,Left,Jump Capabilities for UI buttons in UNITY
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Joystick : MonoBehaviour, IPointerUpHandler, IPointerDownHandler {
private PlayerJoystick playerjoystick;
public GameObject Player;
private float force = 300;
void Start() {
playerjoystick = GameObject.Find ("Player").GetComponent<PlayerJoystick> ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
if(Player!=null)
Player.GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);//in unity5
}
//Move The Player From Keyboard
//transform.position += new Vector3(Input.GetAxis("Horizontal"),0,0);
}
public void OnPointerDown(PointerEventData data) {
if (gameObject.name == "Left") {
playerjoystick.SetMoveLeft (true);
} else if (gameObject.name == "Right") {
playerjoystick.SetMoveLeft (false);
} else {
if(Player!=null)
Player.GetComponent<Rigidbody2D>().AddForce(Vector2.up * force);//Allow Player To Jump
}
}
public void OnPointerUp(PointerEventData data) {
playerjoystick.StopMoving ();
}
} // Joystick
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.