Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

one input does more than one action (problem)

Discussion in 'Scripting' started by Tidcy, Jun 30, 2020.

  1. Tidcy

    Tidcy

    Joined:
    Apr 15, 2020
    Posts:
    53
    hello, I’m a beginner and I’m trying to make the player look inside a chest (in 2d) and see an item that he can pickup. I’ve done it by switching cameras and it’s perfect. One problem:
    In order for the cam that shows the inside of the chest to turn on the player needs to be in the trigger on the chest and press “e”. And in order for the player to pickup the item in the chest he also needs to press “e” while the inside camera is on. So when I press “e” to look inside the box, the item immediately disappears. It takes one input to do both actions and I don’t want that. Is there a way to like delay it or ask for another input? Thx for your time.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    There is a way to do pretty much anything. Without showing code, it's hard to assist you to find out what should be changed.
     
    Tidcy likes this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Use a statemachine. You have one action doing two things, so of course both happen when you press e. Make sure you can only take an item, after the view was changed to the chest inventory view. Also, this must happen in two sequential frames, so if you are already doing that, then you probably call the code for taking the item sequentially after the code opening the chest, which still means that the e-input from this frame enables both actions.
    As Brathnann said tho, without showing us your code that's about as much feedback on your problem as you'll get.
     
    Tidcy likes this.
  4. Tidcy

    Tidcy

    Joined:
    Apr 15, 2020
    Posts:
    53
    Here’s the script on the chest
    Code (Csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class interact : MonoBehaviour
    7. {
    8.  
    9.     public Camera maincam;
    10.     public Camera chestcam;
    11.  
    12.  
    13.  
    14.     void Start()
    15.     {
    16.         maincam.enabled = true;
    17.         chestcam.enabled = false;
    18.  
    19.     }
    20.  
    21.  
    22.     void Update()
    23.     {
    24.  
    25.         if (Input.GetButton("exit") && chestcam.enabled == true)
    26.         {
    27.             maincam.enabled = true;
    28.             chestcam.enabled = false;
    29.  
    30.         }
    31.  
    32.     }
    33.  
    34.  
    35.     void OnTriggerStay2D(Collider2D other)
    36.     {
    37.         if (other.CompareTag("Player") && Input.GetButton("use"))
    38.         {
    39.  
    40.             maincam.enabled = false;
    41.             chestcam.enabled = true;
    42.  
    43.         }
    44.  
    45.  
    46.  
    47.     }
    48. }
    49.  
    I somehow lost the script on the item but it’s something like:
    Code (Csharp):
    1.  
    2. if (chestcam.enabled == true && Input.GetButton(“use”))
    3. {
    4. Item.SetActive(false);
    5. }
    6.  
    And it was written in the update method
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Maybe something like below. It would add a 1 second delay between interact and pickup. There's other ways to do what you want as well. Just wrote these changes quick and dirty ;)

    Code (Csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class interact : MonoBehaviour
    7. {
    8.  
    9.     public Camera maincam;
    10.     public Camera chestcam;
    11.     public float TimeBetweenInteractAndPickup = 1f;
    12.     public static bool AllowPickup = false;
    13.     private float timeSinceInteract = 0f;
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         maincam.enabled = true;
    19.         chestcam.enabled = false;
    20.  
    21.     }
    22.  
    23.  
    24.     void Update()
    25.     {
    26.  
    27.         if (Input.GetButton("exit") && chestcam.enabled == true)
    28.         {
    29.             maincam.enabled = true;
    30.             chestcam.enabled = false;
    31.         }
    32.  
    33.         if (chestcam.enabled)
    34.         {
    35.             if (timeSinceInteract < TimeBetweenInteractAndPickup)
    36.             {
    37.                 timeSinceInteract += Time.deltaTime;
    38.                 if (timeSinceInteract >= TimeBetweenInteractAndPickup)
    39.                 {
    40.                     AllowPickup = true;
    41.                 }
    42.             }
    43.         }
    44.  
    45.     }
    46.  
    47.  
    48.     void OnTriggerStay2D(Collider2D other)
    49.     {
    50.         if (other.CompareTag("Player") && Input.GetButton("use"))
    51.         {
    52.  
    53.             maincam.enabled = false;
    54.             chestcam.enabled = true;
    55.             timeSinceInteract = 0f;
    56.             AllowPickup = false;
    57.         }
    58.  
    59.  
    60.  
    61.     }
    62. }
    63.  

    Code (Csharp):
    1.  
    2. if ((chestcam.enabled == true) && (Input.GetButton(“use”)) && (interact.AllowPickup))
    3. {
    4.     Item.SetActive(false);
    5.     interact.AllowPickup = false;
    6. }
    7.  
     
    Tidcy likes this.
  6. Tidcy

    Tidcy

    Joined:
    Apr 15, 2020
    Posts:
    53
    Thank you so much! The solution is simple and easy to understand!
    But if u don’t mind explaining what this line does?
    Code (Csharp):
    1.  
    2. [LIST=1]
    3. [*]if (timeSinceInteract < TimeBetweenInteractAndPickup)
    4. [*]            {
    5. [*]                timeSinceInteract += Time.deltaTime;
    6.  
    [/LIST]
     
  7. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    Hes using a timer to make sure the button is only pressed once in a given amount of time.
     
    Joe-Censored likes this.
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you are talking about specifically this line:
    Code (csharp):
    1. timeSinceInteract += Time.deltaTime;
    It is a shorthand way of writing this:
    Code (csharp):
    1. timeSinceInteract = timeSinceInteract + Time.deltaTime;
     
    Tidcy likes this.