Search Unity

Visible 'sound blast'

Discussion in 'Scripting' started by ashley, May 7, 2017.

  1. ashley

    ashley

    Joined:
    Nov 5, 2011
    Posts:
    84
    Hi all,

    I'm looking to have the player be able to blast out a sound on button press that would affect enemies nearby.

    I've got a CircleCastAll working to do this with the colliders and that's fine, but I want to be able to visualise it for the player. To make matters more complicated, the radius will decrease with usage (until such time as it has no use).

    I set up an animation that simply increases a sprite to a fixed scale over time. I can't figure out how to change the end keyframe variables manually through script (i.e. when triggering the animation I pass in a 'percent' so if its 100 it would be 1,1,1 and if its 50 it would be 0.5,0.5,0.5).

    I'm aware I could probably use gizmos, but is there any way of giving them more 'flare' so its not just a wireframe?

    Open to any suggestions really, doesn't have to be anything I've said above.

    Thanks!
     
  2. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Is your animation keyframed (made in Unity animation utility) or entirely from code?
     
  3. ashley

    ashley

    Joined:
    Nov 5, 2011
    Posts:
    84
    I was using keyframes but you just saying that made me check the scripting documentation for 'keyframe' and figured it out.

    What I have now is as follows:

    Code (CSharp):
    1. public class SoundRing : MonoBehaviour {
    2.  
    3.     public Animation animation;
    4.  
    5.     void Awake () {
    6.         animation = GetComponent<Animation> ();
    7.     }
    8.  
    9.     public void ExpandRing(float _size)
    10.     {
    11.         AnimationCurve curve = AnimationCurve.Linear (0f, 0f, 1f, _size);
    12.         AnimationClip clip = new AnimationClip ();
    13.         clip.legacy = true;
    14.         clip.SetCurve ("", typeof(Transform), "localScale.x", curve);
    15.         clip.SetCurve ("", typeof(Transform), "localScale.y", curve);
    16.         animation.AddClip (clip, "blast");
    17.         animation.Play ("blast");
    18.  
    19.     }
    20. }
    Then using another script I pass in the float value and it works as expected.

    Let me know if this isn't what you were going to suggest (always good to learn other ways) or if there's any improvements you can recommend.

    Thanks :)
     
  4. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Well generating clip from code is unusual workflow, what I wanted to suggest is interpolating between different transform.localScale values using Vector3.Lerp. Maybe your version is even better than that, since to use Vector3.Lerp you have to keep track of custom timer in the Update() loop (which will serve as interpolation value) and refresh it each time the blast is initiated.
     
    Last edited: May 7, 2017
  5. ashley

    ashley

    Joined:
    Nov 5, 2011
    Posts:
    84