How can I let stuff reset in the script when touching a button
Sorry if I couldn't explain that right, what I want to know is how can I make a few lines of code reset or go back to how it was when touching something.I'm using OnPointerDown and OnPointerUp triggers so I made my game so you press on the right side of the screen and it moves right and the same for left so both triggers for each side , I made another Button and putted it in the middle of the other buttons (middle of the screen) so when you slide over it when you want to go the other way it would reset the OnPointerDown and OnPointerUp but it doesn't work like that it would go in one direction if you press the buttons to fast. Here is the code. So is there anyway to reset the OnPointers so it doesn't bug out when swiping across the screen to move?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
public void MoveLeftButton(string moveleft)
{
Player.horizVel = -3;
}
public void MoveRightButton(string moveright)
{
Player.horizVel = 3;
}
public void MoveLeftButtonStop(string moveleftstop)
{
Player.horizVel = 0;
}
public void MoveRightButtonStop(string moverightstop)
{
Player.horizVel = 0;
}
}
Answer by UnityCoach · Aug 22, 2017 at 08:39 PM
You could, and probably should, rather use IDragHandler.OnDrag, over the whole area, and use the pointer drag Data position to move and or reset based on the coordinates.
Answer by tablekorner · Aug 22, 2017 at 09:05 PM
I'm going to assume that you have seperate scripts for each directional button. Or some way to differentiate between which direction to go for both buttons.
There isn't any need to reset OnPointerDown, and OnPointerUp as the events are designed so the actions in them are only happing if Pointer is down, or if Pointer is up.
Keep in mind that OnPointerUp will also work in the scenerio where you pressed down on a button, dragged off the button while holding down your press, then released off of the button, this is viable to still call OnPointerUp. If this is not the functionality you want, then I'd suggest looking into OnPointerEnter, and OnPointerExit instead. Or a combination of OnPointerEnter, OnPointerExit, and OnPointerDown.
My suggestion would be to have a script on your buttons with this:
public bool mouseOff = false;
public void OnPointerEnter (PointerEventData eventData)
{
mouseOff = true;
}
public void OnPointerExit (PointerEventData eventData)
{
mouseOff = false;
}
public void OnPointerDown (PointerEventData eventData)
{
mouseOff = true;
}
This would allow you to check if button is pressed, or if you press off button, and drag into it. But will also ensure to return false if you press, and drag off the button.
Then you could do sometihng using your functions like this:
if (someObject.GetComponent<WhateverScriptHasOnPointerFunctions>().mouseOff)
{
if (someObject.name == "SomeButtonLeft")
{
// Call your MoveLeftButton function here
}
}
else
{
if (someObject.name == "SomeButtonLeft")
{
// Call your MoveLeftButtonStopfunction here
}
}