Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class AppleTree : MonoBehaviour {
- private bool currentDirection = true; //True is Right, Left is False
- public float speed = 20;
- private int changeRoll = 1;
- private int currentRoll;
- private int frameCounter;
- public GameObject apple;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- frameCounter++;
- Move ();
- ChangeDirection ();
- DropApple ();
- }
- void Move ()
- {
- if (GetComponent<Transform> ().position.x < 9 && (GetComponent<Transform> ().position.x) > -9)
- {
- if (currentDirection == true)
- GetComponent<Rigidbody2D> ().velocity = new Vector3 (speed, 0, 0);
- else if (currentDirection == false)
- GetComponent<Rigidbody2D> ().velocity = new Vector3 (-speed, 0, 0);
- }
- else if (GetComponent<Transform> ().position.x > 9) {
- currentDirection = !currentDirection;
- GetComponent<Rigidbody2D> ().velocity = new Vector3 (-speed, 0, 0);
- }
- else if (GetComponent<Transform> ().position.x < -9)
- {
- currentDirection = !currentDirection;
- GetComponent<Rigidbody2D> ().velocity = new Vector3 (speed, 0, 0);
- }
- }
- void ChangeDirection()
- {
- currentRoll = (int) Random.Range (1, 21);
- if (currentRoll == changeRoll)
- currentDirection = !currentDirection;
- }
- void DropApple()
- {
- if (frameCounter % 30 == 0)
- {
- GameObject newApple = Instantiate (apple) as GameObject;
- newApple.GetComponent<Transform>().position = GetComponent<Transform>().position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement