Move player based on UI controls
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);
*/
}
And this for the button:
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;
}
}
Good day.
Unity answers, its not called Unity code providers.
YOu question shows us you just looked for scripts done by another people without knowing what they work, without searchng what each line code does...
Go learn, dont expect people to give you an aswer that you can easy find in google...
REad the manual of each function you see in your codes and you will get the answers by your own.
Bye
Sure, that helped a lot! :) IF you read the question, i'm just asking how to connect those two scripts, maybe because i didn't find anything on google, but go figure.. And the scripts done by other people is proof that i'm searching and trying to learn. Thank you anyway.
I just say, that using 2 scripts without knowing what they do, will bring you problems all the time.
Its hard to connect 2 scripts if you did not write them... If someone spends time reading, understanding and writing a new code to gives you both scripts connected, you will still get problems in the future cause dont know how they work...