6. Create a new script for the FoodPellet game object by right-clicking in the Scripts folder and selecting Create > C Script. Name it “FoodPelletController”.
7. Add the following code to the FoodPelletController script:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FoodPelletController : MonoBehaviour
{
public float moveSpeed = 2f;
private bool isEaten = false;
void Update()
{
if (!isEaten)
{
transform.position += Vector3.up moveSpeed Time.deltaTime;
}
}
}
8. Attach the FoodPelletController script to the FoodPellet game object in the Hierarchy view.
9. In the PacManController script, add a public Transform variable for the FoodPellet game object and update the code that moves the character to also check if the character is near a food pellet:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PacManController : MonoBehaviour
{
public float moveSpeed = 5f;
public Transform foodPellet;
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow) && transform.position.x > 0)
{
transform.position -= Vector3.right moveSpeed Time.deltaTime;
}
else if (Input.GetKeyDown(KeyCode.RightArrow) && transform.position.x < foodPellet.position.x)
{
transform.position += Vector3.right moveSpeed Time.deltaTime;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y > 0)
{
transform.position -= Vector3.up moveSpeed Time.deltaTime;
}
else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y < foodPellet.position.y)
{
transform.position += Vector3.up moveSpeed Time.deltaTime;
}
}
}
10. In the FoodPelletController script, add a public float variable for the move speed and a private bool variable to track if the food pellet has been eaten:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FoodPelletController : MonoBehaviour
{
public float moveSpeed = 2f;
private bool isEaten = false;
void Update()
{
if (!isEaten)
{
transform.position += Vector3.up moveSpeed Time.deltaTime;
}
}
}
11. In the PacManController script, add a public Transform variable for the FoodPellet game object and update the code that checks if the character is near a food pellet to also check if the food pellet has been eaten:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PacManController : MonoBehaviour
{
public float moveSpeed = 5f;
public Transform foodPellet;
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow) && transform.position.x > 0)
{
transform.position -= Vector3.right moveSpeed Time.deltaTime;
}
else if (Input.GetKeyDown(KeyCode.RightArrow) && transform.position.x < foodPellet.position.x)
{
transform.position += Vector3.right moveSpeed Time.deltaTime;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y > 0)
{
transform.position -= Vector3.up moveSpeed Time.deltaTime;
}
else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y < foodPellet.position.y)
{
transform.position += Vector3.up moveSpeed Time.deltaTime;
}
}
}
12. In the FoodPelletController script, add a public bool variable to track if the food pellet has been eaten:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FoodPelletController : MonoBehaviour
{
public float moveSpeed = 2f;
private bool isEaten = false;
void Update()
{
if (!isEaten)
{
transform.position += Vector3.up moveSpeed Time.deltaTime;
}
}
}