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 /
avatar image
0
Question by loxagos_snake · Sep 18, 2016 at 06:15 PM · interactionadventure

Can't get simple interaction code (Resident Evil style) to work

Hello, fellow devs! I'm working on a prototype for an adventure game, with controls inspired by the early Resident Evil games (also known as 'tank controls'!). What I want to achieve is create a very basic examination system: walk up to an object, press an interaction button ('E', for example), pause the game, and output the item's description - later on, I'm planning to adapt the code to include buttons/levers and puzzles.

The setup is this: I have a simple room, my functional player and a sphere with a trigger collider acting as a placeholder to test my system. The code for the script InteractiveProp is this (I'll provide due explanation below):

 using UnityEngine;
 using System.Collections;
 
 public class InteractiveProp : MonoBehaviour {
 
     public string itemDescription;
 
     private PlayerController playerHandle;
 
     void Start () 
     {
         playerHandle = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
     }
     
     // Handle real time events here (button presses etc.)
     void Update () 
     {
         if (!playerHandle.isInteracting)
         {
             if (Input.GetButtonDown ("Interact"))
             {
                 playerHandle.isInteracting = true;
                 Debug.Log ("Pressed E");
             }
         }
 
         if (playerHandle.isInteracting)
         {
             if (Input.GetButtonDown ("Interact"));
             {
                 playerHandle.isInteracting = false;
                 Debug.Log ("Depressed E");
             }
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Player"))
         {
             Debug.Log ("Player stepped on collider");
 
             if (playerHandle.isInteracting)
             {
                 Time.timeScale = 0;
                 Debug.Log (itemDescription);
             }
         }
     }

InteractiveProp is the name of the script attached to the object I want to be examinable. The string itemDescription contains the item's description, which so far I input through the editor. The bool isInteracting is a member of my player controller script, set to false by default. playerHandle is a reference to the script that I've attached to my player, PlayerController. After initialization in Start(), I'm attempting to detect a button press and switch my bool on or off accordingly, then pass this information to the OnTriggerEnter function. Of course, my player is tagged as 'Player' and in case the button is pressed after the collision, I'm outputting my description to the console (I'll later work on GUI support).

The problem is this: when I move up to the object, I get a debug message that I've successfully touched it. When I press E though, both the 'Pressed E' and 'Depressed E' messages are output simultaneously, resulting in no action. If I try to take the second if in Update() out of the equation to test whether I can interact (my intention is to interact at the first press of E, then if E is pressed again, get out of the examination scene), I get the 'Pressed E' prompt, then if I move away from the object and collide again, the examination system works.

I've been looking at the code for hours and I'm stuck. Please forgive my newbie-ness!

Comment
Add comment · Show 1
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
avatar image loxagos_snake · Sep 18, 2016 at 06:08 PM 0
Share

Alright, I feel stupid. Seems like I added a colon after the second if...I changed the second condition to another key (Escape) and that also seems to imply that both conditions happen simultaneously (both detect the E button press). Could this have something to do with GetButtonDown?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by flashframe · Sep 18, 2016 at 06:40 PM

GetButtonDown will return true for the entire first frame the button is pressed. All the code in your Update loop runs within a single frame, so you only want to detect the button press once.

 if (Input.GetButtonDown ("Interact"))
         {
             if (!playerHandle.isInteracting)
             {
                 playerHandle.isInteracting = true;
                 Debug.Log ("Pressed E");
             }
             else
             {
                 playerHandle.isInteracting = false;
                 Debug.Log ("Depressed E");
             }
         }
Comment
Add comment · Show 1 · Share
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
avatar image loxagos_snake · Sep 18, 2016 at 06:49 PM 0
Share

Hmm, I see. I somehow overlooked that little detail in the documentation. Thank you very much for clearing such an important issue up for me!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Dragging objects like in Amnesia 2 Answers

Prevent rigidbody interaction 1 Answer

How to smoothly interact with doors? 0 Answers

Interacting with gameobjects of same tag 1 Answer

How to make audio interact with visual in VR? VRTK 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