Hello! This is not a question, but more of an understanding. For a few days i've been having problems with the OnPointerDown (it doesn't show in the event trigger) so here is a simple way to make a hold button which I used to make a character move right while holding down the button. (Yes! it works on mobile too)
First you want to add an event trigger to your button via add component, choose 'Add new' and select 'OnPointerDown' and hit the '+' sign and add 'OnPointerUp'. Next put your gameobject (I chose my player which has the tag 'Player') into the object box.
Now for the code
Create a C# script and put it into your player character. (I called mine 'rightmovement')
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class rightmovement : MonoBehaviour, IEventSystemHandler {
public GameObject character;
public float maxSpeed = 10f;
bool buttonHeld = false;
public void pressed (BaseEventData eventData)
{
buttonHeld = true;
}
public void notpressed(BaseEventData eventData)
{
buttonHeld = false;
}
void Start ()
{
character = GameObject.FindWithTag ("Player");
}
public void Update()
{
if (buttonHeld)
{
// Do stuff
Debug.Log ("click");
// Move Character
character.rigidbody2D.velocity = Vector3.right * maxSpeed;
}
else
Debug.Log ("not click");
}
}
Now if you click on the box next to your gameobject in the event trigger, you can choose your script and then the button!. make sure your pressed and not pressed voids are PUBLIC.
I am a complete noob to scripting, but this just happened and I thought I'd share! Enjoy! :)
[1]: /storage/temp/34554-script.png
↧
Trending Articles
More Pages to Explore .....