Question by
Xeladin · Sep 09, 2019 at 12:02 AM ·
2d gameinteractionpoint-and-clickreaction
Having trouble with framework for a 2d point-and-click game
I am trying to make a framework for a point-and-click game. I have a player character, that remains static on the screen, and when an 'Interactable' object is clicked, I have a set of reactions I want to see triggered (some reactions if certain conditions are met, others if they are not). But I'm having trouble getting the interaction to happen, something is wrong with the codes, but I can't put my finger on it.
Player script
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
public class PlayerMovement : MonoBehaviour
{
public float inputHoldDelay = 0.5f; // How long after reaching an interactable before input is allowed again.
private Interactable currentInteractable; // The interactable that is currently being headed towards.
private bool handleInput = true; // Whether input is currently being handled.
private readonly WaitForSeconds inputHoldWait; // The WaitForSeconds used to make the user wait before input is handled again.
// This function is called by the EventTrigger on an Interactable, the Interactable component is passed into it.
public void OnInteractableClick(Interactable interactable)
{
// If the handle input flag is set to false then do nothing.
if (!handleInput)
return;
// Store the interactble that was clicked on.
currentInteractable = interactable;
}
private IEnumerator WaitForInteraction()
{
// As soon as the wait starts, input should no longer be accepted.
handleInput = false;
// Wait for the normal pause on interaction.
yield return inputHoldWait;
// Now input can be accepted again.
handleInput = true;
}
}
Interactable script
using UnityEngine;
// This is one of the core features of the game.
// Each one acts like a hub for all things that transpire
// over the course of the game.
// The script must be on a gameobject with a collider and
// an event trigger. The event trigger should tell the
// player to approach the interactionLocation and the
// player should call the Interact function when they arrive.
public class Interactable : MonoBehaviour
{
public ConditionCollection[] conditionCollections = new ConditionCollection[0];
// All the different Conditions and relevant Reactions that can happen based on them.
public ReactionCollection defaultReactionCollection; // If none of the ConditionCollections are reacted to, this one is used.
// This is called when the player arrives at the interactionLocation.
public void Interact ()
{
// Go through all the ConditionCollections...
for (int i = 0; i < conditionCollections.Length; i++)
{
// ... then check and potentially react to each. If the reaction happens, exit the function.
if (conditionCollections[i].CheckAndReact ())
return;
}
// If none of the reactions happened, use the default ReactionCollection.
defaultReactionCollection.React ();
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612211903im_/https://answers.unity.com/themes/thub/images/avi.jpg)