0
\$\begingroup\$

enter image description hereI've been stuck on this for quite some time. I'm trying to make a platformer game for android so I need some touch buttons. I made some buttons in Photoshop but I can't figure out how to make them move the player right. I've tried implementing the IPointerDownHandler interface but once I get the player moving I can't stop it, even if I call a function in OnPointerUp to set the players velocity to a new vector3 where all values are zero. I've also tried attaching event triggers to the buttons I made, but it's the opposite the player only moves for one frame and then I have to press the button again. I don't want to import any assets from the asset store and I don't want to use gui system. I want to learn how to do it myself. Thanks in advance. Here is the script in which I call functions from my PlayerController.

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class LeftButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{   
    public Image leftButton;

    private PlayerController player;

    private void Start()
    {
        leftButton = GetComponent<Image> ();
        player = FindObjectOfType<PlayerController> ();
    }

    public virtual void OnPointerDown(PointerEventData ped)
    {
        Debug.Log("LeftButton Pressed!!");
        player.moveLeft (-2);
    }

    public virtual void OnPointerUp(PointerEventData ped)
    {
        player.stopMoving ();
        Debug.Log ("Button Lifted!!");
    }
} 

Here is my PlayerController script.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    private Rigidbody2D myrb;

    // Use this for initialization
    void Start () 
    {
        myrb = GetComponent<Rigidbody2D> ();
    }

    // Update is called once per frame
    void Update () 
    {

    }

    public void moveLeft(float moveSpeed)
    {
        myrb.velocity = new Vector3 (moveSpeed, myrb.velocity.y, 0f);
    }

    public void moveRight(float moveSpeed)
    {
        myrb.velocity = new Vector3 (moveSpeed, myrb.velocity.y, 0f);
    }

    public void stopMoving()
    {
        myrb.velocity = new Vector3 (0f, 0f, 0f);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Can you post some of the code you've written? \$\endgroup\$
    – Shraa1
    Oct 15, 2016 at 4:06
  • \$\begingroup\$ Can you show us your PlayerController class too? \$\endgroup\$
    – DMGregory
    Oct 15, 2016 at 13:41

1 Answer 1

1
\$\begingroup\$

You can simply do it by adding two Event Trigger with the UI buttons. One calls a public method for PointerEnter. Another calls a public method for PointerExit.

In the PointerEnter trigger call the method that moves the object and in the PointerExit trigger call the method that stops the movement.

Base event

\$\endgroup\$
9
  • \$\begingroup\$ Thanks I really appreciate it. I'll try it and see if it works. \$\endgroup\$
    – gleechie
    Oct 15, 2016 at 17:57
  • \$\begingroup\$ @DMGregory I've added the PlayerController script. \$\endgroup\$
    – gleechie
    Oct 15, 2016 at 19:22
  • \$\begingroup\$ I tried your method but I still can't stop it from moving once it starts. \$\endgroup\$
    – gleechie
    Oct 15, 2016 at 19:24
  • \$\begingroup\$ Have you assigned the objects and selected the methods correctly? \$\endgroup\$ Oct 15, 2016 at 20:23
  • \$\begingroup\$ I'm not sure what you mean, but I've posted my code you can look it over if you want. \$\endgroup\$
    – gleechie
    Oct 15, 2016 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .