- Home /
Need help In creating android game with UI buttons ?Please Help
I have made these buttons to make this game's controls compatible for android and below is my playermovement script I want to make my UI buttons in screen such that when I use buttons it feels like controlling through two keys of key board
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerMovement : MonoBehaviour {
//This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardforce = 200f;
public float sidewaysforce = 500f;
public float upwardforce = 600f;
public int Start()
{
Scene currentScene = SceneManager.GetActiveScene();
return currentScene.buildIndex;
}
// We marked this as "Fixed Update"because we are using it to mess with physics
public void FixedUpdate() {
rb.AddForce(0, 0, forwardforce * Time.deltaTime); //Add a forward force
if (Input.GetKey("d"))
{
Debug.Log("Right");
Right();
}
if (Input.GetKey("a"))
{
Debug.Log("Left");
Left();
}
if (Start() == 5)
{
if (Input.GetKey("w"))
{
rb.AddForce(0, upwardforce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
public void Right()
{
rb.AddForce(sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
public void Left()
{
rb.AddForce(-sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
Please help me and thanks in advance to whoever helps
Answer by MT369MT · Aug 02, 2018 at 05:18 PM
Hi, create a new script and name it TouchControl and copy-paste this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class TouchControl : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
{
public bool Pressed = false;
public UnityEvent onPressed;
public void OnPointerDown(PointerEventData eventData)
{
Pressed = true;
}
public void OnPointerUp(PointerEventData eventData)
{
Pressed = false;
}
public void OnPointerEnter(PointerEventData eventData)
{
Pressed = true;
}
public void OnPointerExit(PointerEventData eventData)
{
Pressed = false;
}
public void Update()
{
if (Pressed)
{
onPressed.Invoke();
}
}
}
Now add this script to both your buttons and in the inspector there will appear a "On Pressed ()" function with written "List is Empty". Click the + button and there will appear a "None (Object)" field. Drag and drop your player from your Hierarchy and then click on the "No Function" bar, choose Player Movement and then choose your Right () / Left () function.
Ask if you don't understand or you need something else.
Hello Thanks for the Help but I am having one more problem when use your script of TouchControl . It will keep on updating the last button pressed . if I run and level restart it automatically goes to the direction I last pressed .
How do you restart your scene? with Scene$$anonymous$$anager.LoadScene?
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name) this is the line I use to restart the same scene
How is it possible that if you restart the scene it will go to the last pressed direction? If there isn’t something static it shouldn’t do it. Anyway I tried to recreate it in Unity and it works fine for me.