Advertisement
MrSpaceCow

Random Cloud Movement Unity Script 1

Aug 13th, 2016
3,501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CloudScript : MonoBehaviour {
  5.     //Set these variables to whatever you want the slowest and fastest speed for the clouds to be, through the inspector.
  6.     //If you don't want clouds to have randomized speed, just set both of these to the same number.
  7.     //For Example, I have these set to 2 and 5
  8.     public float minSpeed;
  9.     public float maxSpeed;
  10.  
  11.     //Set these variables to the lowest and highest y values you want clouds to spawn at.
  12.     //For Example, I have these set to 1 and 4
  13.     public float minY;
  14.     public float maxY;
  15.  
  16.     //Set this variable to how far off screen you want the cloud to spawn, and how far off the screen you want the cloud to be for it to despawn. You probably want this value to be greater than or equal to half the width of your cloud.
  17.     //For Example, I have this set to 4, which should be more than enough for any cloud.
  18.     public float buffer;
  19.  
  20.     float speed;
  21.     float camWidth;
  22.  
  23.     void Start() {
  24.         //Set camWidth. Will be used later to check whether or not cloud is off screen.
  25.         camWidth = Camera.main.orthographicSize * Camera.main.aspect;
  26.  
  27.         //Set Cloud Movement Speed, and Position to random values within range defined above
  28.         speed = Random.Range(minSpeed, maxSpeed);
  29.         transform.position = new Vector3(-camWidth - buffer, Random.Range(minY, maxY), transform.position.z);
  30.     }
  31.  
  32.     // Update is called once per frame
  33.     void Update() {
  34.         //Translates the cloud to the right at the speed that is selected
  35.         transform.Translate(speed * Time.deltaTime, 0, 0);
  36.         //If cloud is off Screen, Destroy it.
  37.         if(transform.position.x - buffer > camWidth) {
  38.             Destroy(gameObject);
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement