I'm trying to move the player based on UI buttons. I found this topic which covers the button listener: https://forum.unity.com/threads/touch-and-hold-a-button-on-new-ui.266065/#post-1944748
Now I know when the button is being pressed, but how can I "connect" those two scripts to make the player move?
I have this script to the player which was moving without using button.
public class player : MonoBehaviour {
Rigidbody m_Rigidbody;
public float m_Speed;
// Use this for initialization
void Start () {
m_Rigidbody = GetComponent<Rigidbody>();
//Set the speed of the GameObject
}
// Update is called once per frame
void Update () {
/* OLD -> WITHOUT BUTTON
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
x = -x;
m_Rigidbody.velocity = new Vector2( x * m_Speed, y * m_Speed);
*/
}
}
public class scrLeftButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!isPressed) {
return;
}
}
bool isPressed = false;
public void OnPointerDown(PointerEventData eventData){
isPressed = true;
}
public void OnPointerUp(PointerEventData eventData){
isPressed = false;
}
}