Hi, I’m trying to grab/destroy an object when I pressed the “P” key for 1.5 seconds
I have this code, it would work if I don’t ask for “P”, just collide, but the game I’m working on ask for user interactions, any idea of what is wrong?
bool haveAPart = false;
string partName;
int curParts = 0;
float holdCounter = 1.5f;
void OnTriggerEnter(Collider co) {
if ((co.tag == "CarPart") && (haveAPart == false)) { // User can only grab one part at a time
partName = "Hold 'P' to grab " + co.name; // show label with instructions to get the part
if (Input.GetKeyDown (KeyCode.P)) {
holdCounter -= Time.deltaTime;
if (holdCounter <= 0) { // user pressed the "p" key for 1.5 seconds
haveAPart = true;
Destroy (co.gameObject);
partName = "You got: " + co.name;
curParts++;
holdCounter = 1.05f; // restart timer
}
}
}
}
Thanks!