Skip to content

Instantly share code, notes, and snippets.

@pashagray
Last active February 25, 2025 10:02
Show Gist options
  • Save pashagray/8bcc85954676b47542b2f78d09df3d34 to your computer and use it in GitHub Desktop.
Save pashagray/8bcc85954676b47542b2f78d09df3d34 to your computer and use it in GitHub Desktop.
Homework-6-7
using UnityEngine;
public class Ball : MonoBehaviour
{
[SerializeField][Range(1, 100)] private float _force = 5;
[SerializeField] private float _radius = 0.5f;
private Rigidbody _rigidbody;
public float Radius => _radius;
private void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
}
public void ApplyUpForce()
{
_rigidbody.AddForce(Vector3.up * _force, ForceMode.Impulse);
}
public void ApplyLeftForce()
{
_rigidbody.AddForce(Vector3.left * _force, ForceMode.Impulse);
}
public void ApplyRightForce()
{
_rigidbody.AddForce(Vector3.right * _force, ForceMode.Impulse);
}
public void Reset()
{
transform.position = Vector3.zero;
_rigidbody.linearVelocity = Vector3.zero;
}
}
using UnityEngine;
public class GameManager : MonoBehaviour
{
[Header("Boundaries")]
[SerializeField] private float _leftBoundary = -5;
[SerializeField] private float _rightBoundary = 5;
[SerializeField] private float _topBoundary = 5;
[SerializeField] private float _bottomBoundary = -5;
[Header("Scores")]
[SerializeField] private int _upScore = 1;
[SerializeField] private int _leftOrRightScore = 3;
[SerializeField] private int _scoresToWin = 100;
private int _score = 0;
[Header("Scene References")]
[SerializeField] private Ball _ball;
[SerializeField] private TMPro.TextMeshPro _scoresText;
private void Start()
{
DrawBoundaries();
StartNewGame();
}
private void Update()
{
HandleInput();
CheckBoundaries();
CheckWin();
UpdateScoresText();
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.W))
{
_ball.ApplyUpForce();
_score += _upScore;
}
if (Input.GetKeyDown(KeyCode.A))
{
_ball.ApplyLeftForce();
_score += _leftOrRightScore;
}
if (Input.GetKeyDown(KeyCode.D))
{
_ball.ApplyRightForce();
_score += _leftOrRightScore;
}
}
private void CheckBoundaries()
{
if (IsXBoundaryHit() || IsYBoundaryHit())
{
Lose();
}
}
private void CheckWin()
{
if (_score >= _scoresToWin)
{
Debug.Log("You win!");
StartNewGame();
}
}
private void UpdateScoresText()
{
_scoresText.text = _score.ToString();
}
private bool IsXBoundaryHit()
{
return _ball.transform.position.x < _leftBoundary + _ball.Radius ||
_ball.transform.position.x > _rightBoundary - _ball.Radius;
}
private bool IsYBoundaryHit()
{
return _ball.transform.position.y < _bottomBoundary + _ball.Radius ||
_ball.transform.position.y > _topBoundary - _ball.Radius;
}
private void Lose()
{
Debug.Log("You lose!");
StartNewGame();
}
private void StartNewGame()
{
ResetBall();
ResetScore();
}
private void DrawBoundaries()
{
LineRenderer lineRenderer = GetComponent<LineRenderer>();
lineRenderer.positionCount = 5;
lineRenderer.SetPosition(0, new Vector3(_leftBoundary, _topBoundary, 0));
lineRenderer.SetPosition(1, new Vector3(_rightBoundary, _topBoundary, 0));
lineRenderer.SetPosition(2, new Vector3(_rightBoundary, _bottomBoundary, 0));
lineRenderer.SetPosition(3, new Vector3(_leftBoundary, _bottomBoundary, 0));
lineRenderer.SetPosition(4, new Vector3(_leftBoundary, _topBoundary, 0));
}
private void ResetBall()
{
_ball.Reset();
}
private void ResetScore()
{
_score = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment