- Home /
Make object scale pulse smoothly when selected.
I have a cube in my scene and I wish to pulse it's scale when the user points at it to indicate that it is the selected object.
Currently I'm using Mathf.PingPong() which works but it causes the object to suddenly snap to the current ping-pong value when it's first selected. I also would like the object to transition it's scale back to 1 when it's unselected.
Here's what I have so far. SetSelected is called with the appropriate value from another script.
using UnityEngine;
public class NodeObject : MonoBehaviour {
private const float MIN_SCALE = 0.75f;
private bool selected = false;
private GameObject childMesh;
private GameObject childCollider;
// Use this for initialization
void Start () {
childMesh = transform.FindChild("Mesh").gameObject;
childCollider = transform.FindChild("Collider").gameObject;
}
// Update is called once per frame
void Update () {
if (selected) {
float scale = MIN_SCALE + Mathf.PingPong(Time.time * 0.2f, 1f - MIN_SCALE);
childMesh.transform.localScale = new Vector3(scale, scale, scale);
}
}
public bool IsSelected() {
return selected;
}
public void SetSelected(bool value) {
selected = value;
}
}
Answer by Fattie · Jan 24, 2016 at 04:08 PM
Many developers just use iTween for this sort of thing - it's free.
I've actually never used it, and only tried it recently and it's a great time saver.
I used their PunchScale PunchScale
call and it works fine.
I used it to bounce a UI
button, when clicked. I just put this script on anything I want to bounce, and call DoPulse
public class SoundButtonPulse:MonoBehaviour
{
public void DoPulse()
{
System.Collections.Hashtable hash =
new System.Collections.Hashtable();
hash.Add("amount", new Vector3(0.15f, 0.15f, 0f));
hash.Add("time", 1f);
iTween.PunchScale(gameObject, hash);
}
}
Normally I obsessively hand-write bounces and twists, but really it's all you need 99% of the time .. good one, iTween dudes
Thanks. Took me 5 $$anonymous$$utes to get the result I wanted with iTween. Add this as an answer and I'll accept it.
Thank goodness it worked, cheers. I love 5 $$anonymous$$. solutions :O
Your answer
Follow this Question
Related Questions
Changing the size of an animated 2D object 0 Answers
2D Animations Scaling Away From The Floor 0 Answers
blender rigged animation plays, but scaling of the model does not. 0 Answers
DOTween & how to undo SetBlendableScaleBy tweens that get killed 0 Answers
Why does my animation warp when I adjust the scale in Unity? 1 Answer