I want to increase object’s speed only when the button is pressed(touched), it should come back to normal speed when the button is not pressed(touched).
You’re going to have to show more code. Right now what you show doesn’t actually do anything. Speed doesn’t increase there. You’re doing a simple math problem but not assigning the value back to speed.
So, it looks like you have the right idea going. Right now I don’t see you calling SpeedUp, so your speed isn’t going to increase.
So, you just need to apply speedUp somewhere, and then if you want a complete return to 1f, you could do that in TouchPhase.Ended. If you want to reduce over time, you could have a speedDown method that also makes sure that you never go below 1f. That will handle it for you.
That SpeedUp function is assigned to a button UI and it’s working when that button is pressed. And for button we don’t use touch.phase. It works without doing that.
You could try changing from assignment to the button event to using the IPointerDown/IPointerUp interfaces.
Set speed up & down on those.
As @Brathnann mentioned, you could use a slowdown routine if you’d like, also.
This may require that you check that your touch isn’t hitting the button for your other calculations.