- Home /
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!
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?
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");
}
}
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
Follow this Question
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