Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AppleTree : MonoBehaviour {
  6. [Header("Set in Inspector")]
  7. // Prefab for instantiating apples
  8. public GameObject applePrefab;
  9. public GameObject GoldApplePrefab;
  10. public GameObject GreenApplePrefab;
  11. public float chanceToDropSpecial = 0.01f;
  12.  
  13.  
  14. // Speed at which the AppleTree moves
  15. public float speed = 1f;
  16.  
  17. // Distance where AppleTree turns around
  18. public float leftAndRightEdge = 10f;
  19.  
  20. // Chance that the AppleTree will change directions
  21. public float chanceToChangedirections = 0.1f;
  22.  
  23. // Rate at which Apples will be instantiated
  24. public float secondsBetweenAppleDrops = 1f;
  25.  
  26. // Use this for initialization
  27. void Start () {
  28. // Dropping apples every second
  29. Invoke("DropApple", 2f );
  30. }
  31.  
  32. void DropApple() {
  33. GameObject apple = Instantiate<GameObject>(applePrefab);
  34. apple.transform.position = transform.position;
  35. Invoke("DropApple", secondsBetweenAppleDrops);
  36.  
  37. }
  38.  
  39. void DropGreenApple()
  40. {
  41. GameObject GreenApple = Instantiate<GameObject>(GreenApplePrefab);
  42. GreenApple.transform.position = transform.position;
  43. Invoke("DropApple", secondsBetweenAppleDrops);
  44.  
  45. }
  46.  
  47. void DropGoldApple()
  48. {
  49. GameObject GoldApple = Instantiate<GameObject>(GoldApplePrefab);
  50. GoldApple.transform.position = transform.position;
  51. Invoke("DropApple", secondsBetweenAppleDrops);
  52.  
  53. }
  54.  
  55. // Update is called once per frame
  56. void Update() {
  57. if (Random.value < chanceToDropSpecial)
  58. {
  59. DropGreenApple();
  60. DropGoldApple();
  61. }
  62. // Basic Movement
  63. Vector3 pos = transform.position;
  64. pos.x += speed * Time.deltaTime;
  65. transform.position = pos;
  66.  
  67. // Changing Direction
  68. if (pos.x < -leftAndRightEdge) {
  69. speed = Mathf.Abs(speed); //Move right
  70. } else if (pos.x > leftAndRightEdge) {
  71. speed = -Mathf.Abs(speed); //Move left
  72. }
  73. }
  74.  
  75. void FixedUpdate() {
  76. if (Random.value < chanceToChangedirections) {
  77. speed *= -1; // Change direction
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement