I’m using the Ugui and I can’t seem to figure out how to do this. I want to make my character rotate whenever I’m holding down a specific button.(I’m using the standard OnClick events to do this) I have a script that works for rotation and it all works, except I have to keep clicking the rotate button to keep rotating.
I assume there’s an easy way to do this, given that they have an onclick function.
Thanks for your time
Create an Object and add Event Triggers and click " Add New Event Type" and Add Pointerup event and also PointerDown event .
use can use this code for events
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class TouchRace : MonoBehaviour
{
public bool isRacePressed = false;
public bool isbrakePressed = false;
void Start () {
}
void Update () {
if (isRacePressed )
{
//Your code Here
}
else if ( !isRacePressed )
{
//Your Code Here
}
}
public void onPointerDownRaceButton()
{
isRacePressed = true;
}
public void onPointerUpRaceButton()
{
isRacePressed = false;
}
}
The problem is you’re using the standard OnClick event. It’s called on clicks (press and release in the same object). For other events you need an Event Trigger component, and for the special case of holding down, you’ll have to use OnPointerDown and OnPointerUp events. Set a boolean to true when OnPointerDown happens and back to false when OnPointerUp happends. In an Update function rotate on every frame while that bool is set to true.