Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Scaling Z axis of object using Koreographer

Discussion in 'Scripting' started by t_mind, Feb 16, 2021.

  1. t_mind

    t_mind

    Joined:
    Mar 23, 2020
    Posts:
    2
    Hello Unity Forum,

    I'm desperately trying to scale the Z axis of an object, but this code is scaling the whole object equally on all the three axes. Any help would be greatly appriciated.


    Code (CSharp):
    1. using UnityEngine;
    2. namespace SonicBloom.Koreo.Demos
    3. {
    4.     [AddComponentMenu("Koreographer/Demos/Cube Scaler")]
    5.     public class CubeScaler : MonoBehaviour
    6.     {
    7.         [EventID]
    8.         public string eventID;
    9.         public float minScale = 0.5f;
    10.         public float maxScale = 1.5f;
    11.      
    12.         void Start()
    13.         {
    14.             // Register for Koreography Events.  This sets up the callback.
    15.             Koreographer.Instance.RegisterForEventsWithTime(eventID, AdjustScale);
    16.         }
    17.      
    18.         void OnDestroy()
    19.         {
    20.             // Sometimes the Koreographer Instance gets cleaned up before hand.
    21.             //  No need to worry in that case.
    22.             if (Koreographer.Instance != null)
    23.             {
    24.                 Koreographer.Instance.UnregisterForAllEvents(this);
    25.             }
    26.         }
    27.      
    28.         void AdjustScale(KoreographyEvent evt, int sampleTime, int sampleDelta, DeltaSlice deltaSlice)
    29.         {
    30.             float value = 0f;
    31.             if (evt.HasCurvePayload())
    32.             {
    33.                 // Get the value of the curve at the current audio position.  This will be a
    34.                 //  value between [0, 1] and will be used, below, to interpolate between
    35.                 //  minScale and maxScale.
    36.                 value = evt.GetValueOfCurveAtTime(sampleTime);
    37.             }
    38.             else if (evt.HasFloatPayload())
    39.             {
    40.                 value = evt.GetFloatValue();
    41.             }
    42.             transform.localScale = Vector3.one * Mathf.Lerp(minScale, maxScale, value);
    43.         }
    44.     }
    45. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Code (CSharp):
    1. Vector3 scale = transform.localScale;
    2. scale.z = Mathf.Lerp(minScale, maxScale, value);
    3. transform.localScale = scale;
     
    t_mind likes this.
  3. t_mind

    t_mind

    Joined:
    Mar 23, 2020
    Posts:
    2
    This worked, thanks a lot! I gotta up my coding game :D