Itch
I was challenged by my friends to create a Pacman clone as quickly as possible. Although a few features are still missing, they will be added soon! Want to know more about Pacman? it is an iconic video game originally developed by Namco. Learn more about its history on Wikipedia.
For player movement, I used Rigidbody-based movement and the new Player Input system.
using UnityEngine;
using UnityEngine.InputSystem;
[SelectionBase]
public class PlayerMovement : MonoBehaviour
{
private Rigidbody _Rigidbody;
private InputAction _MoveAction;
private PlayerInput _PlayerInput;
[SerializeField] private float _PlayerSpeed;
private void Start()
{
_PlayerInput = GetComponent<PlayerInput>();
_MoveAction = _PlayerInput.actions.FindAction("Move");
_Rigidbody = GetComponent<Rigidbody>();
}
private void Update()
{
CalculateMovement();
}
private void CalculateMovement()
{
Vector2 direction = _MoveAction.ReadValue<Vector2>().normalized;
Vector3 movement = new Vector3(direction.x, 0f, direction.y);
_Rigidbody.velocity = movement * _PlayerSpeed;
}
}
To handle collectible pickups, I used the OnTriggerEnter method to detect collisions with collectible objects.
using UnityEngine;
public class CollectibleHandler : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
GameManager.Instance.GetPointValue(other.GetComponent<CollectibleObject>().GetPoints());
Destroy(other.gameObject);
}
}
}
For enemy movement, I used Unity's NavMesh component. The EnemyManager script controls when the ghosts can move. I added the ghosts to an Enemies array and looped through each of them to enable their NavMeshAgent.
using MyBox;
using UnityEngine;
using UnityEngine.AI;
[SelectionBase] [RequireTag("Enemy")]
public class EnemyMovement : MonoBehaviour
{
private NavMeshAgent _EnemyNavmeshAgent;
public bool CanMove = false;
[SerializeField] private GameObject _Player;
[SerializeField] private string _PlayerString;
private void Start()
{
_EnemyNavmeshAgent = GetComponent<NavMeshAgent>();
FindPlayerByTag(_PlayerString);
}
private void Update()
{
if (CanMove) GoToPlayer();
}
public void GoToPlayer() => GoToDestination(_Player.transform);
private void FindPlayerByTag(string tag)
{
_Player = GameObject.FindGameObjectWithTag(tag);
}
private void GoToDestination(Transform destination)
{
_EnemyNavmeshAgent.SetDestination(destination.position);
}
}