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 SpawnObjects : MonoBehaviour {
- // Chance that the Cube will change directions
- public float chanceToChangedirections = 0.1f;
- // Distance where AppleTree turns around
- public float leftAndRightEdge = 10f;
- public GameObject Cubes; // Apple Object in Scene (Sprite)
- public GameObject PlayerObject; // Bad Apple Object in Scene (Sprite)
- public float spawnTime = 1f; // How long between each spawn.
- public float fallSpeed = 40.0f; //The speed of falling Apples
- private float timer = 0; //counting timer, reset after calling SpawnRandom() function
- private int randomNumber; //variable for storage of an random Number
- void Update()
- {
- timer += Time.deltaTime; // Timer Counter
- if (timer > spawnTime)
- {
- SpawnRandom(); //Calling method SpawnRandom()
- timer = 0; //Reseting timer to 0
- }
- // Basic Movement
- Vector3 pos = transform.position;
- pos.x += fallSpeed * Time.deltaTime;
- transform.position = pos;
- // Changing Direction
- if (pos.x < -leftAndRightEdge)
- {
- fallSpeed = Mathf.Abs(fallSpeed); //Move right
- }
- else if (pos.x > leftAndRightEdge)
- {
- fallSpeed = -Mathf.Abs(fallSpeed); //Move left
- }
- }
- public void SpawnRandom()
- {
- //Creating random Vector3 position
- Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(600, Screen.height), Camera.main.farClipPlane / 2));
- //Instantiation of the Cube Object
- Instantiate(Cubes, screenPosition, Quaternion.identity);
- }
- void FixedUpdate()
- {
- if (Random.value < chanceToChangedirections)
- {
- fallSpeed *= -1; // Change direction
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement