1st Game Using Unity :)

1st Game using Unity :)
Hope you will love it…
Download it from here -
Drive Link — https://drive.google.com/drive/folders/1vFFFuQf2weJYG941v4b429ff7kDzDkxg?usp=sharing
Github Link — https://github.com/karunaket/1st-Game-With-Unity
Watch Video Here — https://vimeo.com/778566957
Today I made a Game using Unity…
PlayerController code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D _rb;
[SerializeField] ObjectBehaviour _objectBehaviour;
float _playerSpeed = 10f;
float _inputHorizontal;
// Start is called before the first frame update
void Start()
{
_rb = gameObject.GetComponent<Rigidbody2D>();
_objectBehaviour.SpawnObject();
}
// Update is called once per frame
void Update()
{
_inputHorizontal = Input.GetAxisRaw("Horizontal");
if (_inputHorizontal != 0)
{
_rb.AddForce(new Vector2(_inputHorizontal * _playerSpeed, 0));
}
}
}
ObjectBehaviour code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectBehaviour : MonoBehaviour
{
[SerializeField] GameObject _prefab;
bool _gameOver = false;
public void SpawnObject()
{
Instantiate(_prefab, new Vector3(Random.Range(-8f, 8f), 9.48f, 0f), Quaternion.identity);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player" && !_gameOver)
{
SpawnObject();
Destroy(gameObject);
}
else if (collision.gameObject.tag == "Ground")
{
_gameOver = true;
Debug.Log("GAME OVER! YOU LOOSE!");
}
}
}
click here to follow me in LinkedIn for more updates…
click here to follow me in GitHub for more updates…