Positional Effects (Scattered Objects Moving Back / Growing Flowers)

 Hey all! 

Earlier this year I made a Quick Tip about a bastion-like effect where tiles move into place when you get near them.  
This post will go a bit more into the same base idea, but different effects and more options. 

On stream this week I showed off an effect from Astro Kat, where parts of a bridge move and also rotate into place. 

When not in place they hover up and down far from the origin 

I want to share the code of this effect with you here

I've also set up an effect where flowers will pop up and glow when you're near them, sharing that as well!

In Astro Kat I've linked the movement to an object you have to carry, in this post both effects are linked to a player character, but you can basically use whatever you want, for example if you want a special bullet that warps thing around it, link it to that :D

The Script

Let's start with the C# script: Pastebin Link 

Psuedocode:

  for (int i = 0; i < Objects Array; i++)
        {
        if(distance to target < radius)
       {
           values[i] = lerp (value , 1, appearspeed);
       } else
       {
             values[i] = lerp (value , 0, disappearspeed);
       }
       >send to value to shader via property block
   }


So in the inspector there is an objects array that where you should drag all objects into that you want to be affected.
The script will check distance from target for all these objects, and lerp a value when its close. Then the value gets sent to a shader with a property block.

With a shader that only outputs the _Moved value from the script, you can see when they turn to value 1 and back to 0

And if you set the bool to "keep" the value will stay at 1.

The script also sends the position of the target, and a random offset for the floating blocks to shaders

 The Growing Flower Shader : PasteBin Link 

Simply scaling the meshes, and adding emission, based on distance from the target

As seen in previous tutorials, the Material Property Block values are set within the instancing buffer code

And to use it, just insert :

UNITY_ACCESS_INSTANCED_PROP(Props, _Moved)

And use it like any other value

Growing

In the vertex function, shrink the mesh when the _Moved value is 0. The "_Shrink" values are added so that the mesh is still visible when the target is away. The values are also saturated so they don't exceed 1 and scale too large.

The additional .xz shrink is to squish the flowers to give more of a blooming effect.

Glowing

In the Surf function, add emission based on the _Moved value. To only make specific parts glow, there's a mask texture (Red Channel)

The  Scattered Move Shader (+Radius Effect): PasteBin Link 

(The texture swap helps it look like a magical effect)

Moving and Rotating

Apart from the _Moved float, this shader also gets a random offset value

Here we add an up and down bobbing motion based on on the _RandomOffset float

And here, depending on a toggle, there is an offset on the X or Y axis

(Setting the Offset and Toggle for X, bobbing motion is happening independant)

In the built-in skybox shader from unity theres a function (RotateAroundYInDegrees) to rotate the vertices around the Y axis, I've used this code here, and a version to rotate around the Z

This function, with the degrees multiplied by the _Moved value, make the mesh rotated when away from the target.

(Setting different angles)

Texture Swapping

Texture swapping in the surface shader is the same as this tip, In the main gif, the dissolve line is set to 0. It has an added scrollspeed value to control the animation of the radius.

Again, here's all the code:

C# Script: PasteBin Link
Blooming Flowers Shader : PasteBin Link
Scattered Move Shader : PasteBin Link 

Have fun!

Tier Benefits
Recent Posts