Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AppleTree : MonoBehaviour {
- [Header("Set in Inspector")]
- // Prefab for instantiating apples
- public GameObject applePrefab;
- public GameObject GoldApplePrefab;
- public GameObject GreenApplePrefab;
- public float chanceToDropSpecial = 0.01f;
- // Speed at which the AppleTree moves
- public float speed = 1f;
- // Distance where AppleTree turns around
- public float leftAndRightEdge = 10f;
- // Chance that the AppleTree will change directions
- public float chanceToChangedirections = 0.1f;
- // Rate at which Apples will be instantiated
- public float secondsBetweenAppleDrops = 1f;
- // Use this for initialization
- void Start () {
- // Dropping apples every second
- Invoke("DropApple", 2f );
- }
- void DropApple() {
- GameObject apple = Instantiate<GameObject>(applePrefab);
- apple.transform.position = transform.position;
- Invoke("DropApple", secondsBetweenAppleDrops);
- }
- void DropGreenApple()
- {
- GameObject GreenApple = Instantiate<GameObject>(GreenApplePrefab);
- GreenApple.transform.position = transform.position;
- Invoke("DropApple", secondsBetweenAppleDrops);
- }
- void DropGoldApple()
- {
- GameObject GoldApple = Instantiate<GameObject>(GoldApplePrefab);
- GoldApple.transform.position = transform.position;
- Invoke("DropApple", secondsBetweenAppleDrops);
- }
- // Update is called once per frame
- void Update() {
- if (Random.value < chanceToDropSpecial)
- {
- DropGreenApple();
- DropGoldApple();
- }
- // Basic Movement
- Vector3 pos = transform.position;
- pos.x += speed * Time.deltaTime;
- transform.position = pos;
- // Changing Direction
- if (pos.x < -leftAndRightEdge) {
- speed = Mathf.Abs(speed); //Move right
- } else if (pos.x > leftAndRightEdge) {
- speed = -Mathf.Abs(speed); //Move left
- }
- }
- void FixedUpdate() {
- if (Random.value < chanceToChangedirections) {
- speed *= -1; // Change direction
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement