Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SpawnObjects : MonoBehaviour {
  6.  
  7. // Chance that the Cube will change directions
  8. public float chanceToChangedirections = 0.1f;
  9. // Distance where AppleTree turns around
  10. public float leftAndRightEdge = 10f;
  11. public GameObject Cubes; // Apple Object in Scene (Sprite)
  12. public GameObject PlayerObject; // Bad Apple Object in Scene (Sprite)
  13. public float spawnTime = 1f; // How long between each spawn.
  14. public float fallSpeed = 40.0f; //The speed of falling Apples
  15. private float timer = 0; //counting timer, reset after calling SpawnRandom() function
  16. private int randomNumber; //variable for storage of an random Number
  17.  
  18.  
  19. void Update()
  20. {
  21. timer += Time.deltaTime; // Timer Counter
  22. if (timer > spawnTime)
  23. {
  24. SpawnRandom(); //Calling method SpawnRandom()
  25. timer = 0; //Reseting timer to 0
  26. }
  27.  
  28.  
  29. // Basic Movement
  30. Vector3 pos = transform.position;
  31. pos.x += fallSpeed * Time.deltaTime;
  32. transform.position = pos;
  33.  
  34. // Changing Direction
  35. if (pos.x < -leftAndRightEdge)
  36. {
  37. fallSpeed = Mathf.Abs(fallSpeed); //Move right
  38. }
  39. else if (pos.x > leftAndRightEdge)
  40. {
  41. fallSpeed = -Mathf.Abs(fallSpeed); //Move left
  42. }
  43. }
  44.  
  45. public void SpawnRandom()
  46. {
  47.  
  48. //Creating random Vector3 position
  49. Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(600, Screen.height), Camera.main.farClipPlane / 2));
  50. //Instantiation of the Cube Object
  51. Instantiate(Cubes, screenPosition, Quaternion.identity);
  52. }
  53.  
  54.  
  55. void FixedUpdate()
  56. {
  57. if (Random.value < chanceToChangedirections)
  58. {
  59. fallSpeed *= -1; // Change direction
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement