Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnTriggerEnter and GetKeyDown C#

Discussion in 'Scripting' started by vanutella, Sep 29, 2017.

  1. vanutella

    vanutella

    Joined:
    Jun 6, 2017
    Posts:
    10
    Hello! I'm working on a game and I'm struggling with triggers. Here's what should be happening:
    The player enters a trigger. When inside the trigger he press 'e'. An animation should start playing.
    My script isn't working and I want to use multiple triggers in my game; each for a different animation of different objects. Here's my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class onEnterAnim : MonoBehaviour {

    private Animator anim;

    private void Start()
    {
    anim = GetComponent<Animator>();
    }
    private void OnTriggerEnter(Collider other)
    {
    if (other.CompareTag("Player"))
    {
    if (Input.GetKeyDown("e"))
    {
    anim.Play("FridgeDoor");
    }
    }
    }
    }

    I put the script on the trigger obejct but it isn't working. the player tag ist set as well as the is trigger.

    Or is there a way to convert this JS Script to C#? (
    )
    I really tried searching in Google but I'm new to C# and couldn't find a solution that suits me.

    Thank you!
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That's because you'd need to be lucky and hit the key in the exact one physics update in which you Enter the trigger. This is not impossible, but the probability is not very high as well.

    The first thing to think about is the OnTriggerStay method, however, keep in mind:

    Physics-based callbacks run in the physics update, which does not happen in a 1:1 ratio to the Update methods. The result is unreliable input behaviour during the physic updates. You may miss input.

    What you should probably do is using the Enter method to enable your Update-based input and the Exit method to disable it.
     
    james_kelleher likes this.
  3. Jildert

    Jildert

    Joined:
    Feb 15, 2010
    Posts:
    33
    Something like this would work like Suddoha explained. I personally don't really like the fact that in this
    case you are handling input inside an item like a chest or in this case the fridgedoor trigger. I would rather let the player know that he is inside of a trigger. And let all the input be handle by the player in 1 class. So when you press E the player says to all triggers he's in that they should do their action.

    But just to get started and not too overflow you with information the code below fixes your current script.

    Code (CSharp):
    1. public class onEnterAnim : MonoBehaviour {
    2.     private Animator anim;
    3.     private bool hasPlayer;
    4.  
    5.     private void Start() {
    6.         anim = GetComponent<Animator>();
    7.     }
    8.  
    9.     private void Update() {
    10.         if (hasPlayer && Input.GetKeyDown("e")) {
    11.             anim.Play("FridgeDoor");
    12.         }
    13.     }
    14.  
    15.     private void OnTriggerEnter(Collider other) {
    16.         if (other.CompareTag("Player")) {
    17.             hasPlayer = true;
    18.         }
    19.     }
    20.  
    21.     private void OnTriggerExit(Collider other) {
    22.         if (other.CompareTag("Player")) {
    23.             hasPlayer = false;
    24.         }
    25.     }
    26. }
     
    b4cksp4ce likes this.
  4. vanutella

    vanutella

    Joined:
    Jun 6, 2017
    Posts:
    10
    It's still not working. :/
     
  5. vanutella

    vanutella

    Joined:
    Jun 6, 2017
    Posts:
    10
    Maybe there's a way to detect when the player (or controller in vr) is near an object? so I can use something like distance from my controller to a gameobject and check if it's <= xx ?
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The trigger solution should work. There must be an issue with your setup of the scene or some other settings.
    But I agree with @Jildert, once you get this to work you should probably think about handling input somewhere else and not in these scripts.
     
  7. KonniosGames

    KonniosGames

    Joined:
    Dec 22, 2018
    Posts:
    18
    Is that right?

    void OnTriggerEnter(Collider other)
    {
    if (Input.GetKeyDown(KeyCode.E))
    {
    anim.SetTrigger("OpenDoor");
    anim.enabled = true;
    }
    }
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    This won't work in most cases, because 1) you shouldn't check Input in any part of the physics-cycle and 2) Enter will only be called in a single run.

    You should add a helper field, for instance a boolean, that you set to true upon entering, and false upon exiting. Then, use that helper field in Update to check whether you're currently in that trigger. If so, you check the Input additionally.