Create a simple Monster Chase 2D game using Unity C#. It includes backgrounds, monsters, and players to create a game environment where scripting for player movement and collision detection is done:
- Player
- CameraFollow
- Collector
- Monster
- GameManager
- GameplayUIController
- MainMenuController
- MonsterSpawner
Monster Chase
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] // SerializeField forces the private variable property to only on inspectar tab
private float moveForce = 10f;
[SerializeField]
private float jumpForce = 11f;
private float movementX;
private Rigidbody2D myBody;
private SpriteRenderer sr;
private Animator anim;
private string WALK_ANIMATION = "Walk";
private bool isGrounded;
private string Ground_TAG = "Ground";
private string ENEMY_TAG = "Enemy";
private void Awake()
{
myBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();
}
void Update()
{
PlayerMoveKeyboard();
AnimatePlayer();
PlayerJump();
}
void PlayerMoveKeyboard()
{
movementX = Input.GetAxisRaw("Horizontal");
transform.position += new Vector3(movementX, 0f, 0f) * Time.deltaTime * moveForce;
}
void AnimatePlayer()
{
if (movementX > 0)
{
// Player Right Movement & Animation
anim.SetBool(WALK_ANIMATION, true);
sr.flipX = false;
} else if (movementX < 0)
{
// Player Left Movement & Animation
anim.SetBool(WALK_ANIMATION, true);
sr.flipX = true;
} else
{
// Player Movement & No Animation
anim.SetBool(WALK_ANIMATION, false);
}
}
void PlayerJump()
{
if(Input.GetButtonDown("Jump") && isGrounded)
{
isGrounded = false;
myBody.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag(Ground_TAG))
isGrounded = true;
if(collision.gameObject.CompareTag(ENEMY_TAG))
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag(ENEMY_TAG))
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
private Transform player;
private Vector3 tempPos;
[SerializeField]
private float minX, maxX;
void Start()
{
player = GameObject.FindWithTag("Player").transform;
}
void LateUpdate()
{
// Edit -> Project settings -> Time -> Fixed Timestep -> 0.02 (default)
// LateUpdate run after the Update method run.
// LateUpdate method is used to control the camera smooth movement by following the player.
if (!player) return; // when player is destroy due to collision with enemy or something then this code runs.
tempPos = transform.position;
tempPos.x = player.position.x;
if (tempPos.x < minX) tempPos.x = minX;
if (tempPos.x > maxX) tempPos.x = maxX;
transform.position = tempPos;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collector : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Enemy") || collision.CompareTag("Player"))
Destroy(collision.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monster : MonoBehaviour
{
[HideInInspector]
public float speed;
private Rigidbody2D myBody;
void Awake()
{
myBody = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
myBody.velocity = new Vector2(speed, myBody.velocity.y);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
[SerializeField]
private GameObject[] characters;
private int _charIndex;
public int CharIndex
{
get { return _charIndex; }
set { _charIndex = value; }
}
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject); // to keep Game Manager obj on Gameplay scene
} else
{
Destroy(gameObject); // destroy the duplicate obj if available there
}
}
private void OnEnable()
{
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
}
// delegates & events
void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
{
if(scene.name == "Gameplay")
Instantiate(characters[CharIndex]);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameplayUIController : MonoBehaviour
{
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name); // Instead of SceneManager.LoadScene("Gameplay");
}
public void HomeButton()
{
SceneManager.LoadScene("MainMenu");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenuController : MonoBehaviour
{
public void PlayGame()
{
int selectedCharacter = int.Parse(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name);
GameManager.instance.CharIndex = selectedCharacter;
SceneManager.LoadScene("Gameplay"); // Navigating "Gameplay" scene
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterSpawner : MonoBehaviour
{
[SerializeField]
private GameObject[] monsterReference;
private GameObject spawnedMonster;
[SerializeField]
private Transform leftPos, rightPos;
private int randomIndex, randomSide;
void Start()
{
StartCoroutine(SpawnMonsters());
}
IEnumerator SpawnMonsters()
{
while(true)
{
yield return new WaitForSeconds(Random.Range(1, 5));
randomIndex = Random.Range(0, monsterReference.Length);
randomSide = Random.Range(0, 2);
spawnedMonster = Instantiate(monsterReference[randomIndex]);
if(randomSide == 0)
{
// left side
spawnedMonster.transform.position = leftPos.position;
spawnedMonster.GetComponent<Monster>().speed = Random.Range(4, 10);
} else
{
// right side
spawnedMonster.transform.position = rightPos.position;
spawnedMonster.GetComponent<Monster>().speed = -Random.Range(4, 10);
spawnedMonster.transform.localScale = new Vector3(-1f, 1f, 1f); // x-axis: -1f for fliping the monster
}
}
}
}
Any Question / Leave a comment ?
--- Thank you for your attention! ---