- Home /
 
 
               Question by 
               FutureLights · Mar 19, 2019 at 10:17 PM · 
                2dsprites2d animation  
              
 
              2D Cloud Animation
I would like to animate 2D clouds (sprites) so they sway a bit in the wind. Does anyone have any idea how to do this? I thought about making it move randomly but then the cloud could eventually move too far from their original location. I am using them as obstacles so they could only move a little bit. If I used hardcoded paths, would it be too noticeable?
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by highpockets · Mar 19, 2019 at 10:48 PM
You could just code it to do something random within a certain range with a couple floats.
 float maxYMove = 2.0f;
 float maxXMove = 3.0f;
 Vector2 intitialColudPos;
 Vector2 targetPos;
 float timeCount = 0.0f;
 
 void Start(){
 initialCloudPos = cloud.transform.position;
 }
 
 void Update(){
 if(timeCount == 0.0f){
 targetPos = new Vector2(Random.Range(initialCloudPos.x - maxXMove, initialCloudPos.x + maxXMove), Random.Range(initialCloudPos.y - maxYMove, initialCloudPos.y + maxYMove)
 }
 
 cloud.transform.position = Vector2.Lerp(cloud.transform.position, targetPos, timeCount);
 
 if(timeCount < 1.0f){
 timeCount = timeCount + Time.deltaTime;
 }else
 {
 timeCount = 0.0f;
 }
 }
 
               This will give you a new randomized location within your limited maxXMove and maxYMove and it will move to this new location every 1 second. If you want to slow it down, you can add a speed variable into the lerp time scale.
Your answer