2nd Game Using Unity :)

2nd Game Using Unity :)

Today I made my 2nd 2D game. It's kind of a shooting game where we can move the player using the W, A, S, D, or arrow keys, and the mouse can be used to point at the enemy and while clicking the mouse, it shoots...

Codes:

GameManager code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class GameManager : MonoBehaviour
{
? ? public bool _gameOver = false;
}        

PlayerController code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PlayerController : MonoBehaviour
{
? ? [SerializeField] GameManager _gameManager;


? ? Rigidbody2D _rb;
? ? Camera _mainCamera;


? ? float _moveVertical;
? ? float _moveHorizontal;
? ? float _moveSpeed = 5f;
? ? float _speedLimiter = 0.7f;
? ? Vector2 _moveVelocity;


? ? Vector2 _mousePos;
? ? Vector2 _offset;


? ? [SerializeField] GameObject _bullet;
? ? [SerializeField] GameObject _bulletSpawn;


? ? bool _isShooting = false;
? ? float _bulletSpeed = 15f;


? ? // Start is called before the first frame update
? ? void Start()
? ? {
? ? ? ? _rb = gameObject.GetComponent<Rigidbody2D>();
? ? ? ? _mainCamera = Camera.main;
? ? }


? ? // Update is called once per frame
? ? void Update()
? ? {
? ? ? ? _moveHorizontal = Input.GetAxisRaw("Horizontal");
? ? ? ? _moveVertical = Input.GetAxisRaw("Vertical");


? ? ? ? _moveVelocity = new Vector2(_moveHorizontal, _moveVertical) * _moveSpeed;


? ? ? ? if (Input.GetMouseButtonDown(0))
? ? ? ? {
? ? ? ? ? ? _isShooting = true;
? ? ? ? }
? ? }


? ? void FixedUpdate()
? ? {
? ? ? ? MovePlayer();
? ? ? ? RotatePlayer();


? ? ? ? if (_isShooting)
? ? ? ? {
? ? ? ? ? ? StartCoroutine(Fire());
? ? ? ? ? ? /*?
? ? ? ? ? ? ? ?I am using coroutine because after certain amount of time the bullet should get distroyed automatically...
? ? ? ? ? ? */
? ? ? ? }
? ? }


? ? void MovePlayer()
? ? {
? ? ? ? if (_moveHorizontal != 0 || _moveVertical != 0)
? ? ? ? {
? ? ? ? ? ? if (_moveHorizontal != 0 || _moveVertical != 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? _moveVelocity *= _speedLimiter;
? ? ? ? ? ? }
? ? ? ? ? ? _rb.velocity = _moveVelocity;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? _moveVelocity = new Vector2(0f, 0f);
? ? ? ? ? ? _rb.velocity = _moveVelocity;
? ? ? ? }
? ? }


? ? void RotatePlayer()
? ? {
? ? ? ? _mousePos = Input.mousePosition;
? ? ? ? Vector3 screenPoint = _mainCamera.WorldToScreenPoint(transform.localPosition);
? ? ? ? _offset = new Vector2(_mousePos.x - screenPoint.x, _mousePos.y - screenPoint.y).normalized;


? ? ? ? float angle = Mathf.Atan2(_offset.y, _offset.x) * Mathf.Rad2Deg; // Rad2Deg --> Radian to degree
? ? ? ? // If we have the offset, it will figure out the angle between them...


? ? ? ? transform.rotation = Quaternion.Euler(0f, 0f, angle - 90f);
? ? ? ? // Euler --> Rotate something to certain angle...
? ? }


? ? IEnumerator Fire()
? ? {
? ? ? ? _isShooting = false;
? ? ? ? GameObject bullet =? Instantiate(_bullet, _bulletSpawn.transform.position, Quaternion.identity);
? ? ? ? bullet.GetComponent<Rigidbody2D>().velocity = _offset * _bulletSpeed;


? ? ? ? yield return new WaitForSeconds(3);
? ? ? ? Destroy(bullet);


? ? ? ? // After 3 seconds the bullet will get distroyed automatically...
? ? }
}        

EnemyBehaviour code:

using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.SceneManagement;


public class EnemyBehaviour : MonoBehaviour
{
? ? GameManager _gameManager;
? ? GameObject _player;


? ? float _enemyHealth = 100f;
? ? float _enemyMoveSpeed = 2f;
? ? Quaternion _targetRotation;
? ? bool _disableEnemy = false;
? ? Vector2 _moveDirection;


? ? private ScoreManager scoreManager;




? ? // Start is called before the first frame update
? ? void Start()
? ? {
? ? ? ? scoreManager = GameObject.Find("Canvas").GetComponent<ScoreManager>();
? ? ? ? _gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
? ? ? ? _player = GameObject.FindGameObjectWithTag("Player");
? ? }


? ? // Update is called once per frame
? ? void Update()
? ? {
? ? ? ? if (!_gameManager._gameOver && !_disableEnemy)
? ? ? ? {
? ? ? ? ? ? MoveEnemy();
? ? ? ? ? ? RotateEnemy();
? ? ? ? }
? ? }


? ? void MoveEnemy()
? ? {
? ? ? ? transform.position = Vector2.MoveTowards(transform.position, _player.transform.position,?
? ? ? ? ? ? _enemyMoveSpeed * Time.deltaTime);
? ? }


? ? void RotateEnemy()
? ? {
? ? ? ? _moveDirection = _player.transform.position - transform.position;
? ? ? ? _moveDirection.Normalize();


? ? ? ? _targetRotation = Quaternion.LookRotation(Vector3.forward, _moveDirection);


? ? ? ? if (transform.rotation != _targetRotation)
? ? ? ? {
? ? ? ? ? ? transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, 200 * Time.deltaTime);
? ? ? ? }
? ? }


? ? private void OnCollisionEnter2D(Collision2D collision)
? ? {
? ? ? ? if (collision.gameObject.tag == "Bullet")
? ? ? ? {


? ? ? ? ? ? StartCoroutine(Damaged());


? ? ? ? ? ? _enemyHealth -= 40f;


? ? ? ? ? ? if (_enemyHealth <= 0f)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Destroy(gameObject);
? ? ? ? ? ? ? ? scoreManager.score += 1f;
? ? ? ? ? ? }


? ? ? ? ? ? Destroy(collision.gameObject);
? ? ? ? }
? ? ? ? else if (collision.gameObject.tag == "Player")
? ? ? ? {
? ? ? ? ? ? _gameManager._gameOver = true;
? ? ? ? ? ? collision.gameObject.SetActive(false);
? ? ? ? }
? ? }


? ? IEnumerator Damaged()
? ? {
? ? ? ? _disableEnemy = true;
? ? ? ? yield return new WaitForSeconds(0.5f);
? ? ? ? _disableEnemy = false;
? ? }
}        

EnemySpawn code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class EnemySpawn : MonoBehaviour
{
? ? [SerializeField] GameManager _gameManager;
? ? [SerializeField] GameObject[] _spawnPoints;
? ? [SerializeField] GameObject _enemy;
? ? float _spawnTimer = 2f;
? ? float _spawnRateIncrease = 5f;


? ? // Start is called before the first frame update
? ? void Start()
? ? {
? ? ? ? StartCoroutine(SpawnNextEnemy());
? ? ? ? StartCoroutine(SpawnRateIncrease());
? ? }


? ? IEnumerator SpawnNextEnemy()
? ? {
? ? ? ? int nextSpawnLocation = Random.Range(0, _spawnPoints.Length);


? ? ? ? Instantiate(_enemy, _spawnPoints[nextSpawnLocation].transform.position, Quaternion.identity);


? ? ? ? yield return new WaitForSeconds(_spawnTimer);


? ? ? ? if (!_gameManager._gameOver)
? ? ? ? {
? ? ? ? ? ? StartCoroutine(SpawnNextEnemy());
? ? ? ? }
? ? }


? ? IEnumerator SpawnRateIncrease()
? ? {
? ? ? ? yield return new WaitForSeconds(_spawnRateIncrease);


? ? ? ? if (_spawnTimer >= 0.5f)
? ? ? ? {
? ? ? ? ? ? _spawnTimer -= 0.2f;
? ? ? ? }


? ? ? ? StartCoroutine(SpawnRateIncrease());
? ? }
}        

ScoreManager code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SocialPlatforms.Impl;


public class ScoreManager : MonoBehaviour
{
? ? public TMP_Text textscore;
? ? public float score;


? ? // Start is called before the first frame update
? ? void Start()
? ? {
? ? ? ? score = 0f;
? ? ? ? textscore.text = "KILLS : " + score.ToString();
? ? }


? ? // Update is called once per frame
? ? void Update()
? ? {
? ? ? ? textscore.text = "KILLS : " + score.ToString();
? ? }
}        

That's all...Thank you for reading??

要查看或添加评论,请登录

Karuna Ketan的更多文章

社区洞察

其他会员也浏览了