// Use this for initialization void Start() { spinRate = Random.Range(-1.0f, 1.0f) * spinRate; if (xDirection != 0) { if (xDirection > 0) // . . . . . . . . . . . . . . . Set initial direction { xDriftDir = driftdirs.right; } else if (xDirection < 0) { xDriftDir = driftdirs.left; } else { xDriftDir = driftdirs.right; } } else { xDriftDir = driftdirs.none; } if (yDirection != 0) { float rnd = Random.Range(-1, 1); if (rnd > 0) { yDriftDir = driftdirs.up; } else if (rnd < 0) { yDriftDir = driftdirs.down; } else { yDriftDir = driftdirs.up; } } else { yDriftDir = driftdirs.none; } currXPos = this.transform.position.x; currYPos = this.transform.position.y; sprtRndr = this.GetComponent <SpriteRenderer>(); rgdBdy = this.GetComponent <Rigidbody2D>(); xDirection /= 100; yDirection /= 100; rgdBdy.angularVelocity = (spinRate + Random.Range(0.1f, -0.1f)); inUse = true; }
void Start() // * * * * * * * * * * * * * * * * * * Pre-initialising { this.transform.SetParent(GameObject.Find("Manyoclouds").transform); spinRate = Random.Range(-0.1f, 0.1f); if (xDirection > 0) // . . . . . . . . . . . . . . . Set initial direction { xDriftDir = driftdirs.right; } else if (xDirection < 0) { xDriftDir = driftdirs.left; } else { xDriftDir = driftdirs.right; } float rnd = Random.Range(-1, 1); if (rnd > 0) { yDriftDir = driftdirs.up; } else if (rnd < 0) { yDriftDir = driftdirs.down; } else { yDriftDir = driftdirs.up; } xDirection /= 100; yDirection /= 100; alphaDriftTimer = 0; currXPos = this.transform.position.x; currYPos = this.transform.position.y; sprtRndr = this.GetComponent <SpriteRenderer>(); rgdBdy = this.GetComponent <Rigidbody2D>(); rgdBdy.angularVelocity = (spinRate + Random.Range(0.1f, -0.1f)); sprtRndr.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); alphaIncreasing = true; sprtRndr.sprite = sprites[Random.Range(sprites.Count - 1, 0)]; inUse = true; }
void Update() // * * * * * * * * * * * * * * * * Update { if (inUse) // . . . . . . . . . . . . . . . . Disable object based on position and camera position { if (((this.transform.position.y - sprtRndr.bounds.size.y) < (Camera.main.ScreenToWorldPoint(new Vector3(0.0f, 0.0f, 0.0f)).y - 15.0f)) || ((this.transform.position.y + sprtRndr.bounds.size.y) > (Camera.main.ScreenToWorldPoint(new Vector3(Screen.height, 0.0f, 0.0f)).y + 70.0f))) { sprtRndr.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); Disable(); } if (((this.transform.position.x - sprtRndr.bounds.size.x) > (Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, 0.0f)).x + 1.0f)) || ((this.transform.position.x + sprtRndr.bounds.size.x) < (-10.0f))) { sprtRndr.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); Disable(); } if (inUse) { float tmpXFrc = 0.0f; float tmpYFrc = 0.0f; if (alphaIncreasing) // . . . . . . . . . . . . . Fade in/out based on time { if (alphaDriftTimer < alphaDriftTime) { alphaDriftTimer += Time.deltaTime; } else { alphaIncreasing = false; } sprtRndr.color = new Color(alphaDriftTimer / alphaDriftTime, alphaDriftTimer / alphaDriftTime, 1.0f, alphaDriftTimer / alphaDriftTime * maxAlpha); } else { } switch (xDriftDir) // . . . . . . . . . . . . . Move in direction { case driftdirs.right: if (rgdBdy.velocity.x < xVelLimit) { tmpXFrc += Random.Range(speeds.x, 0); } break; case driftdirs.left: if (rgdBdy.velocity.x > (-xVelLimit)) { tmpXFrc -= Random.Range(speeds.x, 0); } break; } switch (yDriftDir) { case driftdirs.up: if (rgdBdy.velocity.y < yVelLimit) { tmpYFrc += Random.Range(speeds.y, 0); } break; case driftdirs.down: if (rgdBdy.velocity.y > (-yVelLimit)) { tmpYFrc -= Random.Range(speeds.y, 0); } break; } if (this.transform.position.x >= (currXPos + xDrift)) // . . . . . Change direction based on follow target { xDriftDir = driftdirs.left; } else if (this.transform.position.x <= (currXPos - xDrift)) { xDriftDir = driftdirs.right; } if (this.transform.position.y >= (currYPos + yDrift)) { yDriftDir = driftdirs.down; } else if (this.transform.position.y <= (currYPos - yDrift)) { yDriftDir = driftdirs.up; } rgdBdy.AddForce(new Vector2(tmpXFrc, tmpYFrc)); currXPos += xDirection; currYPos += yDirection; } } }
// Update is called once per frame public virtual void Update() { float tmpXFrc = 0.0f; float tmpYFrc = 0.0f; switch (xDriftDir) // . . . . . . . . . . . . . Move in direction { case driftdirs.right: if (rgdBdy.velocity.x < xVelLimit) { tmpXFrc += Random.Range(xDriftRate, 0); } break; case driftdirs.left: if (rgdBdy.velocity.x > (-xVelLimit)) { tmpXFrc -= Random.Range(xDriftRate, 0); } break; default: break; } switch (yDriftDir) { case driftdirs.up: if (rgdBdy.velocity.y < yVelLimit) { tmpYFrc += Random.Range(yDriftRate, 0); } break; case driftdirs.down: if (rgdBdy.velocity.y > (-yVelLimit)) { tmpYFrc -= Random.Range(yDriftRate, 0); } break; default: break; } if (xDriftDir != driftdirs.none) { if (this.transform.position.x >= (currXPos + xDrift)) // . . . . . Change direction based on follow target { xDriftDir = driftdirs.left; } else if (this.transform.position.x <= (currXPos - xDrift)) { xDriftDir = driftdirs.right; } } if (yDriftDir != driftdirs.none) { if (this.transform.position.y >= (currYPos + yDrift)) { yDriftDir = driftdirs.down; } else if (this.transform.position.y <= (currYPos - yDrift)) { yDriftDir = driftdirs.up; } } if ((yDriftDir != driftdirs.none) && (xDriftDir != driftdirs.none)) { rgdBdy.AddForce(new Vector2(tmpXFrc, tmpYFrc)); currXPos += xDirection; currYPos += yDirection; } }