Skip to content

Instantly share code, notes, and snippets.

@IvanEnginer
Created September 7, 2024 16:06
Show Gist options
  • Select an option

  • Save IvanEnginer/20733d27e47a746fbe67fd700b4d75ec to your computer and use it in GitHub Desktop.

Select an option

Save IvanEnginer/20733d27e47a746fbe67fd700b4d75ec to your computer and use it in GitHub Desktop.
using UnityEngine;
public class BirdInBoundaryGame : MonoBehaviour
{
[SerializeField] private Bird _bird;
[SerializeField] private float _upperYLimit;
[SerializeField] private float _lowerYLimit;
[SerializeField] private float _rightXLimit;
[SerializeField] private float _leftXLimit;
[SerializeField] private string _looseMessage;
[SerializeField] private int _winCriterion;
[SerializeField] private Transform _upperBorder;
[SerializeField] private Transform _lowerBorder;
[SerializeField] private Transform _rightBoard;
[SerializeField] private Transform _leftBoard;
private bool _isRunning;
private void Start()
{
StartGame();
}
private void Update()
{
if (Input.GetKey(KeyCode.F))
StartGame();
if (_isRunning == false)
return;
if (_bird.transform.position.y > _upperYLimit || _bird.transform.position.y < _lowerYLimit)
{
_bird.gameObject.SetActive(false);
Debug.Log(_looseMessage);
Debug.Log($"Your score: {_bird.JumpCount}");
_isRunning = false;
}
if(_bird.JumpCount > _winCriterion)
{
_bird.gameObject.SetActive(false);
Debug.Log($"You win, your score: {_bird.JumpCount}");
_isRunning = false;
}
}
private void StartGame()
{
_isRunning = true;
_bird.gameObject.SetActive(true);
_bird.transform.position = new Vector3(0,0,0);
_bird.ResetVelocity();
_bird.ResetJumpCounter();
_upperBorder.position = new Vector3(0, _upperYLimit, 0);
_lowerBorder.position = new Vector3(0, _lowerYLimit, 0);
_rightBoard.position = new Vector3(_rightXLimit, 0, 0);
_leftBoard.position = new Vector3(_leftXLimit, 0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment