Hello! I’ve put together this code, but the OnTriggerStay commands only work around 50% of the time (including the debug log message). The Update command, on the other hand, work every time I press the E key. I do not believe it’s due to the collider (the player) leaving the trigger, as the collider is completely stationary both when working correctly and not. Anyone know why? Thanks
EDIT: It seemingly works a lot less when I turn on “Maximize On Play”. I have absolutely no idea why.
EDIT 2: It also works 100% of the time when the project is actually built and ran. Not sure why that is, but I suppose it’s not as big of an issue then.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moistbutton : MonoBehaviour
{
public GameObject moist;
public AudioSource audioSource;
public Animation press;
public void OnTriggerStay(Collider collision)
{
if (Input.GetKeyUp(KeyCode.E))
{
Debug.Log("moist");
press.Play("moist");
audioSource.Play();
}
}
void Update()
{
if (Input.GetKeyUp(KeyCode.E))
{
Debug.Log("m");
}
}
}
Trigger stay should work exactly the same way in the editor as in built stuff. If it’s not there’s a bug somewhere in how you’re using it or setting it up.
As far as I know, the collisions are handled by the physics side and therefore inputs can be missed as the physics updates are running at a different rate to the update calls. What I usually do is just set a bool and then deal with all input in the update method.
Oh yeah, nice spot there @WarmedxMints_1 … that is almost certainly the problem.
OP: when Mr Mints says “inputs can be missed” he means edge-trigger ones like “Up” and “Down” for instance… those are only guaranteed to detect properly if called in Update().
Here’s a timing diagram and you can see how the inputs could be missed if processed in FixedUpdate() or when colliders are called: