Advertisement
Guest User

AppleTree

a guest
May 28th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AppleTree : MonoBehaviour {
  5.  
  6.     private bool currentDirection = true; //True is Right, Left is False
  7.     public float speed = 20;
  8.     private int changeRoll = 1;
  9.     private int currentRoll;
  10.     private int frameCounter;
  11.     public GameObject apple;
  12.  
  13.     // Use this for initialization
  14.     void Start () {
  15.    
  16.     }
  17.    
  18.     // Update is called once per frame
  19.     void Update () {
  20.         frameCounter++;
  21.         Move ();
  22.         ChangeDirection ();
  23.         DropApple ();
  24.     }
  25.  
  26.     void Move ()
  27.     {
  28.         if (GetComponent<Transform> ().position.x < 9 && (GetComponent<Transform> ().position.x) > -9)
  29.         {
  30.             if (currentDirection == true)
  31.                 GetComponent<Rigidbody2D> ().velocity = new Vector3 (speed, 0, 0);
  32.             else if (currentDirection == false)
  33.                 GetComponent<Rigidbody2D> ().velocity = new Vector3 (-speed, 0, 0);
  34.         }
  35.         else if (GetComponent<Transform> ().position.x > 9) {
  36.             currentDirection = !currentDirection;
  37.             GetComponent<Rigidbody2D> ().velocity = new Vector3 (-speed, 0, 0);
  38.         }
  39.         else if (GetComponent<Transform> ().position.x < -9)
  40.         {
  41.             currentDirection = !currentDirection;
  42.             GetComponent<Rigidbody2D> ().velocity = new Vector3 (speed, 0, 0);
  43.         }
  44.     }
  45.  
  46.     void ChangeDirection()
  47.     {
  48.         currentRoll = (int) Random.Range (1, 21);
  49.         if (currentRoll == changeRoll)
  50.             currentDirection = !currentDirection;
  51.     }
  52.  
  53.     void DropApple()
  54.     {
  55.         if (frameCounter % 30 == 0)
  56.         {
  57.             GameObject newApple = Instantiate (apple) as GameObject;
  58.             newApple.GetComponent<Transform>().position = GetComponent<Transform>().position;
  59.         }
  60.            
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement