Я доделываю игру, там появляются и исчезают два вида объектов - птицы и яблоки.
Вот их код:
Создание птиц:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bird : MonoBehaviour
{
[Header("Set in Inspector")]
public GameObject leftBirdPrefab;
public static float secondsBetweenBird = 10.0f;
[HideInInspector]
private GameObject _birdLeftBottom, _birdLeftTop;
// Start is called before the first frame update
void Awake(){
_birdLeftBottom = GameObject.Find("BirdLeftBottom");
_birdLeftTop = GameObject.Find("BirdLeftTop");
}
void LeftBirdRandom(){
GameObject leftBird = Instantiate(leftBirdPrefab) as GameObject;
leftBird.transform.position = Vector3.Lerp(_birdLeftBottom.transform.position, _birdLeftTop.transform.position, Random.Range(0f, 1f));
Invoke("LeftBirdRandom", secondsBetweenBird);
}
void Start()
{
Invoke("LeftBirdRandom", secondsBetweenBird);
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections.Generic;
using UnityEngine;
public class Bird : MonoBehaviour
{
[Header("Set in Inspector")]
public GameObject leftBirdPrefab;
public static float secondsBetweenBird = 10.0f;
[HideInInspector]
private GameObject _birdLeftBottom, _birdLeftTop;
// Start is called before the first frame update
void Awake(){
_birdLeftBottom = GameObject.Find("BirdLeftBottom");
_birdLeftTop = GameObject.Find("BirdLeftTop");
}
void LeftBirdRandom(){
GameObject leftBird = Instantiate(leftBirdPrefab) as GameObject;
leftBird.transform.position = Vector3.Lerp(_birdLeftBottom.transform.position, _birdLeftTop.transform.position, Random.Range(0f, 1f));
Invoke("LeftBirdRandom", secondsBetweenBird);
}
void Start()
{
Invoke("LeftBirdRandom", secondsBetweenBird);
}
// Update is called once per frame
void Update()
{
}
}
Движение птиц и уничтожение их за пределами экрана:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveBird : MonoBehaviour
{
private Transform _thisTransform;
public static float speedBird = 2.0f;
public static float speedBirdAnimate = 1.0f;
private float _camWidth;
private float _camHeight;
private Animator _moveBirdSpeed;
// Start is called before the first frame update
void Awake(){
_camHeight = Camera.main.orthographicSize;
_camWidth = _camHeight * Camera.main.aspect;
_thisTransform = GetComponent<Transform>();
_moveBirdSpeed = GetComponent<Animator>();
}
void Start()
{
_moveBirdSpeed.speed = speedBirdAnimate;
}
// Update is called once per frame
void Update()
{
_thisTransform.position += new Vector3(speedBird * Time.deltaTime, 0f, 0f);
if (this.transform.position.x > _camWidth)
{
Destroy(this.gameObject);
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class MoveBird : MonoBehaviour
{
private Transform _thisTransform;
public static float speedBird = 2.0f;
public static float speedBirdAnimate = 1.0f;
private float _camWidth;
private float _camHeight;
private Animator _moveBirdSpeed;
// Start is called before the first frame update
void Awake(){
_camHeight = Camera.main.orthographicSize;
_camWidth = _camHeight * Camera.main.aspect;
_thisTransform = GetComponent<Transform>();
_moveBirdSpeed = GetComponent<Animator>();
}
void Start()
{
_moveBirdSpeed.speed = speedBirdAnimate;
}
// Update is called once per frame
void Update()
{
_thisTransform.position += new Vector3(speedBird * Time.deltaTime, 0f, 0f);
if (this.transform.position.x > _camWidth)
{
Destroy(this.gameObject);
}
}
}
Создание яблок:
Синтаксис:
Используется csharp
private GameObject _randomApple1;
private GameObject _randomApple2;
void Awake(){
_randomApple1 = GameObject.Find("RandomApple1");
_randomApple2 = GameObject.Find("RandomApple2");
}
void Start()
{
//Сбрасывать яблоки раз в секунду
Invoke("DropApple", 2f);
}
void DropApple(){
int randomApplePrefab = Random.Range(0, applePrefab.Length);
GameObject randomApple = applePrefab[randomApplePrefab];
GameObject apple = Instantiate(randomApple) as GameObject;
apple.transform.position = Vector3.Lerp(_randomApple1.transform.position, _randomApple2.transform.position, Random.Range(0f, 1f));
Invoke("DropApple", secondsBetweenAppleDrops);
}
Уничтожение яблок:
Синтаксис:
Используется csharp
public class Apple : MonoBehaviour
{
private float _bottomY = -1f;
void Awake()
{
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (transform.position.y < _bottomY)
{
AudioManager.PlaySound(AudioManager.GetClips()[1]);
if (this.gameObject.tag != "bad")
{
if (Basket.life == 0)
{
Basket.life = 0;
Basket.lifeGT.text = Basket.life.ToString();
Debug.Log("end");
}
else
{
Basket.life--;
Basket.lifeGT.text = Basket.life.ToString();
}
}
Destroy(this.gameObject);
}
}
{
private float _bottomY = -1f;
void Awake()
{
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (transform.position.y < _bottomY)
{
AudioManager.PlaySound(AudioManager.GetClips()[1]);
if (this.gameObject.tag != "bad")
{
if (Basket.life == 0)
{
Basket.life = 0;
Basket.lifeGT.text = Basket.life.ToString();
Debug.Log("end");
}
else
{
Basket.life--;
Basket.lifeGT.text = Basket.life.ToString();
}
}
Destroy(this.gameObject);
}
}
На сколько я понимаю, это трудо-затрадные операции.
Вопрос. Как это сделать правильно?
Можно рабочий пример или на одном из моих скриптов?
Благодарю за внимание!