- Home /
How to make a beating/pulsating button effect
So I would like to make a button like this on the image but I would like it to pulsate or beat like a beating heart all the time, even in the idle. The button will be for a mobile game so it should have that effect all the time. Anyone know how I can do this?
Answer by Llama_w_2Ls · Apr 02, 2021 at 09:47 PM
Here's some untested code but it should work. It essentially expands the size of the button rapidly twice, then returns it to its original size, like a heartbeat. You can also control its speed and pulse strength:
public class HeartBeat : MonoBehaviour
{
public float Strength;
public float Speed;
void Start()
{
StartCoroutine(Pulse());
}
IEnumerator Pulse()
{
// Loops forever
while (true)
{
float timer = 0f;
float originalSize = transform.localScale.x;
// Heart beat twice
for (int i = 0; i < 2; i++)
{
// Zoom in
while (timer < 0.1f)
{
yield return new WaitForEndOfFrame();
timer += Time.deltaTime;
transform.localScale = new Vector3
(
transform.localScale.x + (Time.deltaTime * Strength * 2),
transform.localScale.y + (Time.deltaTime * Strength * 2)
);
}
}
// Return to normal
while (transform.localScale.x < originalSize)
{
yield return new WaitForEndOfFrame();
transform.localScale = new Vector3
(
transform.localScale.x - Time.deltaTime * Strength,
transform.localScale.y - Time.deltaTime * Strength
);
}
transform.localScale = new Vector3(originalSize, originalSize);
yield return new WaitForSeconds(Speed);
}
}
}
Hey man, thanks for answering. This works but it has a delay when the button grows and then goes back to normal size like this:
Grow->Back to normal->Delay->Grow->Back to normal...
How to get rid of that delay so the button constantly goes "grow-backtonormal-grow-backtonormal..."?
I believe that you would just set speed to 0, to make sure there's no delay
Yeah that did it lol, didn't even thought about it.
I don't wanna bother you but if you have will and time.. now that button grow looks too fast if you know what I mean, it just pops up and goes back fast so it just doesn't look good going that fast. Even when I play with the Speed and Strength I can't really get it to go slowly up and then slowly down. Do you know how I could make it work more smoothly?
Your answer
Follow this Question
Related Questions
Gui Button zoom 1 Answer
enable Blur effect when Esc button pressed (Windows Phone 8) 1 Answer
Cooldown effect in a button 4 Answers
create a button with name and text for every game object with tag 0 Answers
Changing animation using with UI button 0 Answers