Question by
TheoTheFreak · Jun 05, 2021 at 09:01 AM ·
mechanicspoint-and-click
Is this a good way to implement a Point & Click mechanic?
Hi,
I'm working on a point and click game (my first game), and I'm curious if I implemented the core Point & Click mechanic the right way.
The way it works is that every object in the scene that is selectable has this script attached to it:
pbulic class Selectable : MonoBehaviour { public bool selectable; public Material DefaultMaterial; }
And this is the script that selects an object and is attached to the Main Camera:
The "CurrentSelection" holds the object that the mouse is hovering on, and the "Active" holds the object that the player is clicked on.
public class PointAndClick : MonoBehaviour { [SerializeField] private Material SelectionMaterial; public GameObject CurrentSelection; public GameObject Active; private void Update() { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit) && hit.transform != null) { var SelectedGameObject = hit.transform.gameObject; if (SelectedGameObject.GetComponent () != null) { var Selectable = SelectedGameObject.GetComponent (); if (Selectable.selectable) { CurrentSelection = SelectedGameObject; Select(SelectedGameObject); SetActive(SelectedGameObject); } else { RemoveActive(); } } else { DeSelect(CurrentSelection); CurrentSelection = null; RemoveActive(); } } } private void Select(GameObject gameobject) { var SelectedRenderer = gameobject.GetComponent (); SelectedRenderer.material = SelectionMaterial; } private void DeSelect(GameObject gameobject) { if (CurrentSelection != null) { var DefaultMaterial = gameobject.GetComponent ().DefaultMaterial; gameobject.GetComponent ().material = DefaultMaterial; } } private void SetActive(GameObject gameobject) { if (Input.GetMouseButtonDown(0)) { Active = gameobject; // "gameobject" is selected, do something with it... } } private void RemoveActive() { if (Input.GetMouseButtonDown(0)) { Active = null; } } }
Are there any issues with my code that can be a problem later?
Comment