Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

206 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Rougelike Tutorial-Part 11 Player Does Not Move 0 Answers

how to make levels 1 Answer

How do you attach a script to a game object in Unity 2D version 4.5.1? 1 Answer

script doesnt work as intended. 1 Answer

Background images for 2d games, one sprite vs tiles for simple one color 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges