What I am trying to do is when the player holds down the UI button it continues to rotate an object until the player takes their finger off of the button. The game is a mobile game by the way. Thank you for your help.
throw a script like this onto a UI element than you can feed in the code you want to be run the button is pressed and when it is released.
using UnityEngine;
using UnityEngine.EventSystems;
public class UIBtnActions : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
public void OnPointerDown(PointerEventData eventData) {
// do stuff on Pointer down
}
public void OnPointerUp(PointerEventData eventData) {
// do stuff on pointer up
}
}
1 Like
you’ll have to use the interface system in the new UI. OnPointerDown and OnPointerUp can be used to control a bool logic variable that can determine when you first pressed down, and when you finally lifted up.
here would be a barebones working exemple
using UnityEngine;
using UnityEngine.EventSystems;
public class UIBtnActions : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
[SerializeField] private Transform _objecTransform;
[SerializeField] private float _rotateSpeed;
private bool canRotate = false;
public void OnPointerDown(PointerEventData eventData) {
canRotate = true;
}
public void OnPointerUp(PointerEventData eventData) {
canRotate = false;
}
private void Update() {
if (canRotate) {
_objecTransform.Rotate(Vector3.up * _rotateSpeed * Time.deltaTime);
}
}
}
passerbycmc thank you so much. You have helped a lot.
thank you for your help