Folks, would you please recommend the best way to simulate slight bending of grass and trees?
My grass and trees are 2d sprites, rendered with the sprite renderer and the game uses orthographic camera. I replaced default sprite shader with my own one which skewed vertices by some angle. Well, it kinda works but I’m not satisfied with results.
Looks like I managed to find an answer to this problem myself. The way to go is to play around with mesh vertices or UVs. However looks like it is only possible for regular meshes not sprites. So I had to make all my sprites regular meshes with mesh renderers attached.
After this I attach some script to every instance I’d like to be animated, e.g:
`
using UnityEngine;
using System.Collections.Generic;
public class AnimateTree : MonoBehaviour
{
Mesh mesh;
Vector3[] verts_ref;
Vector3[] verts;
float time_offset;
void Start()
{
mesh = GetComponent<MeshFilter>().mesh;
verts_ref = mesh.vertices;
verts = new Vector3[verts_ref.Length];
System.Array.Copy(verts_ref, verts, verts_ref.Length);
time_offset = Random.Range(0, 10000);
}
void OnBecameInvisible()
{
enabled = false;
}
void OnBecameVisible()
{
enabled = true;
}
void Update()
{
float time = time_offset + Time.time;
float x_tweak = 0.060f;
float y_tweak = 0.0085f;
float val_x = Mathf.Sin(time);
float val_y = Mathf.Cos(time);
float k = 1.0f;
for(int i=(int)(verts.Length*0.4f);i<verts.Length;++i)
{
Vector3 v = verts_ref*;*
if(i%5 == 0) k = 1.0f;
if(i%5 == 1) k = 1.2f;
v.x = v.x + k * x_tweakval_x;
v.y = v.y + k * y_tweakval_y;
verts = v;
}
mesh.vertices = verts;
}
`
Hi Pachash, how you convert your sprites into meshes? thanks!