I am making a pacman clone and i was wondering how i would. respawn pacman to the centre of the maze (14,15) after he has been killed by a ghost.`
public Transform waypoints; // make an array of preset waypoints for the ghost to follow
int cur = 0; // a counter to move through each of the waypoints
public float speed = 0.3f;
// Update is called once per frame
void OnTriggerEnter2D(Collider2D co)
{
if (co.name == "Pacman")// if the ghost collides with pacman
{
if (Health.health > 0)// and the number of lives that pacman has is bigger than 0
{
Destroy(co.gameObject);//destroy the pacman sprite
Health.health -= 1;//remove one from the number of hearts pacman has
Debug.Log("blinky hit pacman");
}
}
}
void FixedUpdate()
{
// Waypoint not reached yet? then move closer
if (transform.position != waypoints[cur].position)
{
Vector2 p = Vector2.MoveTowards(transform.position,waypoints[cur].position,speed);//move towards the next waypoint
GetComponent<Rigidbody2D>().MovePosition(p);
}
// Waypoint reached, select next one
else cur = (cur + 1) % waypoints.Length;
// Animation
Vector2 dir = waypoints[cur].position - transform.position;
GetComponent<Animator>().SetFloat("DirX", dir.x);// change the animation parameters to allow the correct
//animations to play
GetComponent<Animator>().SetFloat("DirY", dir.y);
}`
This is the code i a using to make the Ghosts move around and i would like to re-instantiate pacman inside the OnTriggerEnter2D function