The purpose of this game is to reduce the reputation of the restaurant in order to win the game
create panel for customer request panel
select your canvas then change the render mode to world space, make sure the panel will follow the customer
add text-textmeshpro to show the player text request
add customer to your scene
add collider to customer
attached the customerRequest.cs to your customer
using TMPro;
using UnityEngine;
public enum FoodItem { Salt, ChiliSauce, Fries, Pizza, Burger, None }
public class CustomerRequest : MonoBehaviour
{
public FoodItem requestedItem;
public GameObject requestPanel;
public TextMeshProUGUI requestText;
void Start()
{
SetRandomRequest();
}
public void SetRandomRequest()
{
requestedItem = (FoodItem)Random.Range(0, System.Enum.GetValues(typeof(FoodItem)).Length - 1);
requestText.text = "I want: " + requestedItem.ToString();
requestPanel.SetActive(true);
}
public string requestedItemString()
{
return requestedItem.ToString();
}
}
fill in the slot
Rename your items to this name, case sensitive so must exactly the same.
public enum FoodItem { Salt, ChiliSauce, Fries, Pizza, Burger }
same with this code
because later we will check the item take is same with the gameobject name
if(foodItem.ToString()== hit.collider.gameObject.name)
create a parent "items" to all your items and add Object selector to your items
add collider for all your items
using Unity.VisualScripting;
using UnityEngine;
public class ObjectSelector : MonoBehaviour
{
public Transform handHeld;
static bool isRightHandFull = false;
GameObject holdItem;
public FoodItem foodType;
void Update()
{
// Detect mouse click
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Raycast from the mouse position
if (Physics.Raycast(ray, out RaycastHit hit))
{
// Optional: check for tag
if (hit.collider.CompareTag("Selectable"))
{
Debug.Log("Selected object: " + hit.collider.name);
if (!isRightHandFull)
{
holdItem = Instantiate(hit.collider.gameObject, handHeld);
holdItem.transform.localPosition = Vector3.zero;
Destroy(hit.collider.gameObject);
isRightHandFull = true;
}
}
if (hit.collider.CompareTag("Customer"))
{
CustomerRequest customerRequest = GameObject.FindWithTag("Customer").GetComponent<CustomerRequest>();
isRightHandFull = false;
if (holdItem != null)
{
Debug.Log(customerRequest.requestedItemString());
if (holdItem.gameObject.name.Contains(customerRequest.requestedItemString()))
{
Debug.Log("Thanks, this is what I want");
ReputationSystem.instance.ReduceReputation(+10);
}
else
{
Debug.Log("This is not what I want");
ReputationSystem.instance.ReduceReputation(-10);
}
customerRequest.SetRandomRequest();
}
Destroy(holdItem);
}
}
}
}
}
Change the food type accordingly
Create a slider
Delete the handle
using UnityEngine;
using UnityEngine.UI;
public class ReputationSystem : MonoBehaviour
{
public static ReputationSystem instance;
public Slider reputationSlider;
public float maxReputation = 100f;
public float damagePerWrongItem = 10f;
private float currentReputation;
void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
}
}
void Start()
{
currentReputation = maxReputation;
reputationSlider.maxValue = maxReputation;
reputationSlider.value = currentReputation;
}
public void ReduceReputation(float reputationScore)
{
currentReputation += reputationScore;
reputationSlider.value = currentReputation;
if (currentReputation <= 0)
{
WinGame();
}
}
void WinGame()
{
Debug.Log("You win! Restaurant ruined.");
// Show win UI, pause game, etc.
}
}
select all your items, add Object selector to your items
select all your items and add Selectable tag to it
Make sure your customer have tag Customer
the foodtype for customer is none