- Home /
Unity 5: UI button OnPointerDown not function as expected
I just started learning Unity for few days. I am testing the function of UI Arrow Button which is supposed to be able to translate a car object in x and z position in a 3D scene.
Since my target platform is WebGL, I also coded the keyboard control of arrow button. Currently I wrote 2 scripts for this function. One is attached to the car object and another is attached to the UI Up Arrow Button.
The keyboard control worked fine in PC but I would like to enable the control of translation of the car object also in mobile (though Unity WebGL doesn't officially support mobile platforms). I attempted to use the OnPointerDown() function to get it work but it wasn't even called when I pressed the up button.
How to get the UI arrow button work when it is pressed down?
The Image of the Interface is here.
And my scripts are here:
the one attached to car object
using UnityEngine;
public class CarPlayerControl : MonoBehaviour {
public float speed = 40;
private bool touchCtrl = false;
private float xInput = 0f;
private float zInput = 0f;
void Awake(){
//if (Input.touchSupported && Application.platform
// != RuntimePlatform.WebGLPlayer){
//touchCtrl = true;
//}
}
void FixedUpdate(){
xInput = 0f;
zInput = 0f;
if (touchCtrl)
{
//if (Input.GetButton("ButtonUp"))
//{
// xInput = 1.0f;
// Debug.Log("button up " + xInput);
//}
//else if (Input.GetButton("ButtonDown"))
//{
// xInput = -1.0f;
// Debug.Log("button down " + xInput);
//}
//else if (Input.GetButton("ButtonLeft"))
//{
// zInput = 1.0f;
// Debug.Log("button left " + zInput);
//}
//else if (Input.GetButton("ButtonRight"))
//{
// zInput = -1.0f;
// Debug.Log("button right " + zInput);
//}
}
else //mouse
{
xInput = Input.GetAxis("Horizontal");
zInput = Input.GetAxis("Vertical");
}
Move(xInput, zInput);
}
public void Move(float xInput, float zInput)
{
float xMove = xInput * speed * Time.deltaTime;
float zMove = zInput * speed * Time.deltaTime;
float x = KeepXWithinRange(xMove);
float y = transform.position.y;
float z = KeepZWithinRange(zMove);
transform.position = new Vector3(x, y, z);
}
private float KeepXWithinRange(float xMove){
float x = transform.position.x + xMove;
return Mathf.Clamp(x, 0, 900);
}
private float KeepZWithinRange(float zMove)
{
float z = transform.position.z + zMove;
return Mathf.Clamp(z, -470, 500);
}
}
and the one attached to the UI Up arrow button
using UnityEngine;
public class ButtonUpControl : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public GameObject car;
private CarPlayerControl carPlayerControl;
private bool mouseDown;
private float xInput;
void Awake()
{
carPlayerControl = car.GetComponent<CarPlayerControl>();
mouseDown = false;
}
void Start()
{
}
void FixedUpdate()
{
if (mouseDown) {
xInput = 1.0f;
}
else {
xInput = 0f;
}
carPlayerControl.Move(xInput, 0f);
}
public void OnPointerDown(PointerEventData eventData)
{
mouseDown = true;
Debug.Log("on ptr down btn up ");
}
public void OnPointerUp(PointerEventData eventData)
{
mouseDown = false;
Debug.Log("on ptr up btn up ");
}
}
Answer by InsomniakDev · Feb 17, 2020 at 07:09 PM
I realise this old but it might help someone,
the fastest way here is to add an EventTrigger component to your GameObject, add the events you need, and redirect their actions on your custom scripts.
Your answer
Follow this Question
Related Questions
Add Listeners to array of Buttons 2 Answers
Unity - keep created buttons after quit 1 Answer
I can't click to an UI button. 3 Answers
How to keep your button selected after clicking away ??? 1 Answer