- Home /
I need to puck up object using a key, but something is wrong with code
I need my player to pick up object with "soupcan" tag, it worked before I write a " Input.GetKeyDown" I hope you will help me
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pick : MonoBehaviour
{
void OnTriggerEnter(Collider col)
{
if (col.tag=="soupcan" && Input.GetKey(KeyCode.E))
{
Destroy(col.gameObject);
}
}
}
Try using OnTriggerStay? Not 100% about this but my guess could be that TriggerEnter is only happening at the instance the soup can enters the collider so unless you're already pressing the key down before you touch the soupcan the enter trigger has already occurred by the time you press it after touching it.
Answer by rh_galaxy · May 07, 2020 at 01:30 PM
OnTriggerEnter is only called in a single frame (one time), at the exact time of collision. When you add GetKey they both must be true at the exact same time - which they are not. You can use OnTriggerStay to get a longer time period so you have a chanse to press the E key.
Your answer
Follow this Question
Related Questions
Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers
How to stop Camera from going into colliders 3 Answers
Collision issues with game object 1 Answer
Get the script instance associated with a collider 1 Answer
Raycast from inside an object 2 Answers