Make a "button" GameObject to grow and get smaller over x seconds, OnMouseEnter, C#

Hi there, I was trying to make a feedback effect for when I'm over my menu buttons (say, simple 3D GameObjects with their respective colliders). I know I have to use the OnMouseOver function. My idea was to try and rescale the vectors up to a certain size (using +=), but I just couldn't make it stop growing.

If you could give me a little help, I'd appreciate it. :)

this is my OnMouseOver function.

    Vector3 ButtonScale; //will contain the original GO scale.
    Vector3 NewScale; // will contain the scale that'll be edited.

    void Start () {
    ButtonScale = this.transform.localScale; //stores original GO scale
    NewScale = ButtonScale; //it stores the original scale to edit from here 

    void OnMouseOver() {
        NewScale += new Vector3(1f,0f,1f); //edit the variable
        this.transform.localScale = NewScale; //uses the new value to rescale the GO
    }

I know this is quite incomplete, but since I just cannot make it work so far, I didn't want to keep going.

Hi,

I would suggest two possible approach:

1: using the pingpong math function built in Unity.

2: a more complete, utterly cool tool is iTween.

iTween will make it very easy for you to animate your scale. It has numerous features including exhaustive easing function so that your animation look very lively. It also has pingpong like feature for animation.

Check the documentation, it has built in function to animate the scale, look at ScaleTo()

Here is a working example (javascript)

public var ScaleTo:Vector3 = Vector3(1.5,1.5,1.5);

private var OriginalScale:Vector3;

public function Start(){

OriginalScale = transform.localScale;

}

public function OnMouseEnter(){

    iTween.Stop(gameObject);
    iTween.ScaleTo(gameObject,{"scale":ScaleTo,"time":0.2,"loopType":iTween.LoopType.pingPong});
}

public function OnMouseExit(){

    iTween.Stop(gameObject);
    iTween.ScaleTo(gameObject,OriginalScale,0.3);

}

Bye,

Jean

Here are some tips:

  • Use camelCase, not PacsalCase, for variable names.
  • You don't have to use the this keyword in order to reference transforms, as your script to derives from MonoBehavior, which inherits from Component.
  • If you can store values in the Editor, do that, instead of doing it at the beginning of the game. (The "Custom Reset" function I'm including can be accessed via the script's drop-down menu, for example.)
  • Cache the trasnform for better performance.

.

using UnityEngine;
using UnityEditor;

public class ButtonScale : MonoBehaviour {

[HideInInspector] [SerializeField] float growRate;
[HideInInspector] [SerializeField] new Transform transform;
[HideInInspector] [SerializeField] Vector3 originalScale, mouseOverScale;

#if UNITY_EDITOR
[SerializeField] float duration;
[SerializeField] Vector3 mouseOverMultiplier;

[ContextMenu("Custom Reset")] void Reset () {
    growRate = 1 /duration;
    transform = GetComponent<Transform>();
    originalScale = transform.localScale;
    mouseOverScale = Vector3.Scale(originalScale, mouseOverMultiplier);
    enabled = false;
}
#endif

float rate, lerp;

void OnMouseEnter () {
    rate = growRate;
    enabled = true;
}

void OnMouseExit () {
    rate = -growRate;
    enabled = true;
}

void Update () {
    lerp += rate * Time.deltaTime;
    transform.localScale = Vector3.Lerp(originalScale, mouseOverScale, lerp);
    if (lerp <= 0 || lerp >= 1) {
        lerp = Mathf.Clamp01(lerp);
        enabled = false;
    }
}

}