Hello guys,
I am here to ask you help about a thing that is bothering me from literally a week. Here is the problem:
I have to put animations and charging of things (like particles) with the pressure of a touch button but I can’t figure out how. I’ve tried CoRoutines, Loops, Booleans inside other methods, playing with Input.Touch (etc.) and so on but I can’t find a solution.
The code is too long, about 100 lines only for a serie of if statements in regard of the pressure and release of a button because there are lots of things going on; so I think it is better to post an example of what I mean:
If(//Here I want to put the touch code that must work with a Canvas button but I can't comprehend how)
{
playerAnimation.Play("Particle")
}
All of this in the Update method, but I want to put THIS at work with touch buttons.
With them the only things that I can do are single taps while the thing that I want are long presses.
The buttons to be touched are made with Canvas, just to remember.
With a normal key everything works fine.
THANK YOU so much if you bother my question.
Thank you for your response but as I said, I’ve tried everything.
I studied thoroughly the documentation but I can’t put that to work. Is for this precise reason that I’m asking help.
The fact is, I can’t put a long pressure on a Canvas button, even with the touch phases because I can’t figure out how to give a position, to the touch, where the button is.
Not exactly.
I want to play an animation, or something that have to last long, with a long pressure touch.
The code that I want to execute is all inside the Update method. The problem is that I can’t put that to work with a long pressure touch; with the keyboard works fine. I put single touches (or brief touches or single taps, you get the idea) outside the Update method and they work fine; when it comes to long touches nothing works.
You can get the position of a touch. And if you wanted, you could use a combination of things. Touch for the pressure and the ipointerdown/up interfaces to detect touching a button.
But honestly, I might be confused as to what you want also. From the sounds of it, you have a button on the screen and you want to do different things based on a quick tap, a tap and hold.
So, trigger the button and set a bool. While that bool is true, let your update check for touch if you need to. If you’re using the touch pressure system. Then when you let go of the button, set the bool to false.
I try to make things clear.
I have a character that, with a long button pressure, plays a certain animation and do much other stuff.
The code is inside the update function within an if statement.
I have buttons on the screen, two that move the player on the x axis and two on the y axis; other than those I have different buttons that do different actions like shoot and jump for example. I have ALSO a button that makes the player play a precise animation, but this animation works only if you press the button long enough.
The problem is this: I can’t put to work long touches, not with other methods, Loops, Booleans, IF statements, calling the OnGUI method, references, Touch code and so on.
The question is, how can I play a long animation with a long pressure touch located on the position of the Canvas button?
This is enough, the other stuff inside the if statement isn’t to consider.
Thanks.
I’d suggest using IPointerDown and begin a timer. If the time (long press) you want passes, do the animation. Otherwise, cancel the timer.
You could be running the timer in Update() or a coroutine.
The confusion I’m having is the “long button pressure” if you just want to do something while the button is down, what I mention is enough. If you want the touch pressure system, then you’ll need to integrate that as well, but you can use that in combination with what I said above. This is the touch pressure Unity - Scripting API: Touch.pressure
If you need both, you use the pointer system to detect you’ve pressed the button, then you check your touch pressure.
Oh, and as @methos5k says, run a timer if you want different things to happen after a set time, then either trigger as x time passes or check that timer when you let go of the button.
Ya, sorry, I wasn’t that familiar with the pressure of touch (not used it or participated in a lot of talks about it).
“long pressure” could mean either, I suppose
I’m not either. Which is why I was trying to get clarification if he wanted the pressure or he just wanted how long a person is holding down the button. Since you asked about certain amount of time and OP said not exactly, I wasn’t sure.
Thank you for your time.
The problem, however, remains.
Is obvious that without a script it can be difficult to give an idea, so I try to explain in another way:
I want a specific CanvasButton to, if pressed for long enough (with touch code), to play an animation that need a certain amount of time to be completed. This, however, won’t work in any case that I’ve tried. So, how can I create a touch.position in a particular point of the screen (in this case the button is a circle)?
If it’s a button on the screen, then that’s what we’re explaining. Using the ipointer interfaces, you can push the button, start a timer/set a bool to true, and then when the button is released, you stop the timer/set bool to false.
This way, while the button is pressed, the timer is going or the bool is true and you just use that to determine what you want to do. You don’t need to use the Input.touch code, the ipointer interfaces handle touches or clicks.
With what you are asking, it sounds like you want to start a timer, then as it ticks, check the value. Once it hits timer >= x amount, trigger your animation.
The script with the ipointer interfaces on it would go on your button. This way only that button reacts to that down/up.
Note that you don’t actually need a button UI object. It can be an image, or even an image that has alpha set to 0. Just needs the raycast enabled on it.
To explain better perhaps…
Player presses button, the OnPointerDown registers the press and turns on timer.
Player is holding finger against the button. Timer ticks. Some code checks the time passed.
If required time passes, animation players.
If player removes finger, timer stops.
If player presses and holds finger again, timer resets and starts again.
Even with your method things don’t work.
The fact is, the animation is something that you must play on the Update method, right? So with another method the only thing that Unity do is a frame of the animation. I’ve tried with a timer and even with a timer and a set of bools, but nothing changed.
Thanks.
I’m using the animation system and, obviously, the animator.
I need to play that specific animation in a certain amount of time.
Moreover, I have in this code even a particle that will spread depending on the time you are pressing the button.
I can’t post the code because there’s lot of staff that isn’t matter of this question and will confuse more. I’ll try to make an example:
There is a lot more going on, but this is an example of the essential code that I need to execute in order to work with the other.
With the keyboard it works perfectly, but if I start putting touch code it doesn’t work nor with timers, booleans inside other methods, methods, OnGUI, loops, references, buttons, IPointers, event system and so on.
Everything of this code doesn’t work.
Even in the simplest scenario, if you set the ‘isCharging’ to true when the IPointerDown event occurs, and to false when ‘up’ occurs, it should work. If you have some code that says "else isCharging = false’ (related to your GetKey), you’d have to remove that, too, or work around it.
Not sure how you wrote the other code that you say doesn’t work…
Well, the thing is: I’ve made another script dedicated ONLY to this exact problem, therefore without other code that can interfere. It doesn’t work anyway.
I’ll ask you a question (thank you for your time however), how you’ll write a code like this made with a touch system in mind? Maybe if you show me what you mean even with few lines of code I can see if we’re thinking the same thing (although I think we are).
I think I already gave an example of what I’d do, as did @Brathnann .
I would set a bool with IPointerDown and set my timer to 0. Then, with the help of the Update method or a coroutine, I would check against this bool and increment the timer (and whatever else). The first frame that the time is >= “long press time”, I would start the animation (on a loop if you expect it to repeat before it’s done).
On PointerUp , I would set the bool to false.