MovingController

So, let's create a new C# script and name it UI_MovingController. First of all, we need to add a using-statement so we can use the Handlers for the Unity Event System. To do so, add this line before the declaration of the class:

using UnityEngine;
using UnityEngine.EventSystems;
public class UI_MovingController : MonoBehaviour {
//[...]
}

Then, in the declaration of the class itself, we need to implement two interfaces to intercept every time that this button is pressed or released. As a result, we can determine when the button is held down. The first interface is IPointerDownHandler, whereas the second one is IPointerUpHandler. You can write them after the inheritance from MonoBehaviour, as shown in the following code:

public class UI_MovingController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
//[...]
}

Before we implement the interface, we need to add a couple of variables. The first one is to store the reference to the Movement Component of our character. The second variable is the direction, which indicates if this is the button that goes right or left, respectively placing a value of +1 or -1. These must be private variables, but serializable so that they can be set in the Inspector:

[SerializeField]
private MovementComponent movementComponent;

[SerializeField]
private float direction = +1f;

Also, we need a third variable to store whether the button is held down, which needs to be private:

private bool isHolding;

Now, in the Awake() function, we need to check if we are running in a mobile environment. If not, we destroy the whole button from the interface, since we don’t need to show it anymore:

void Awake() {
//Destroy this script in case we are not running on mobile
#if !(UNITY_STANDALONE || UNITY_EDITOR)
Destroy(gameObject);
#endif
}

In the Awake() function, we can also check that the reference to the Movement Component is properly set:

void Awake() {
//Destroy this script in case we are not running on mobile
#if !(UNITY_STANDALONE || UNITY_EDITOR)
Destroy(gameObject);
#endif

//Disable this script in case the Movement Component reference is not set and leave an error message.
if (movementComponent == null) {
Debug.LogError("Missing reference on MovementComponent on " + gameObject.name + " to run the Controller. Please add the reference.");
Destroy(this);
}
}

Next, we can implement our interfaces. In the first one, with the OnPointerDown() function, we just need to set isHolding to true:

public void OnPointerDown(PointerEventData eventData) {
isHolding = true;
}

Similarly, for the second interface with the function OnPointerUp(), we set isHolding to false:

public void OnPointerUp(PointerEventData eventData) {
isHolding = false;
}

Finally, in the LateUpdate() function (LateUpdate() because the Movement Component uses Rigidbody2D; that's why we don't use the Update() function instead, we pass the direction to the Movement Component if isHolding is set to true:

private void LateUpdate() {
if (isHolding) {
movementComponent.MoveCharacter(direction);
}
}
As you can see, we always pass a value of one. The axis implementation of the keyboard instead passes increasing values up to one (similarly when the key is released). This gives smoother movements. As an exercise, you can try to implement something similar for this button by taking into consideration for how long it has been pressed. 

Save the script and add it to both the buttons. Remember to reference the Movement Component, as well as placing the right direction for the button (+1 for right and -1 for left). This is the setup for the left button:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset