Search code examples
c#unity-game-enginecubes

Squeezing cubes in unity


I want my cubes in the background to smoothly scale up and down randomly with time , like that by Brackeys in party killer . Currently my code scales all of them up then down. My code : transform.localscale = new Vector3 (1f,Mathf.Sin(Time.time),1f);

It scales all of them up and down uniformly whereas i want randomness like that in video.

Any help?


Solution

  • What you're looking for is an offset value. To give them each a different starting position try the following:

    
    float freqOffset;
    
    void Start(){
        freqOffset = Random.value * Mathf.PI * 2;
    }
    void Update(){
        float theta = freqOffset + Time.time;
        float y = Mathf.Sin(theta);
        transform.localscale = new Vector3 (1f,y,1f);
    }
    

    Note that multiplying by two pi will give you an even distribution of values. Also if you want them to move them at different speeds try multiplying 'Time.time' by another random value.