- Home /
VR : How to check if object held passed through trigger Zone
Hey there, I'm quite new to unity and i'm trying to learn the ropes for VR game development.
Is there a way to make it so the object detected in the collision is the object i'm holding and not myself ?
I'm trying to create a trigger zone that when i enter it with a specific object in my hand in spawns something.
For now my trigger zone spawns something each time i pass through, and i'm trying to use tags to correct that, without much success, has my collider is always my hand and not the object i'm holding... Do you have any idea on how to correct that ?
here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnBottle : MonoBehaviour
{
// Start is called before the first frame update
public Transform Spawnpoint;
public GameObject Prefab;
public bool EnteredTrigger;
public GameObject m_bottle;
void Start(){
//m_bottle = GameObject.FindGameObjectWithTag("Bottle");
}
void update() {
if(EnteredTrigger){
Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation);
}
}
void OnTriggerEnter(Collider other){
if(other.name == "Bottle"){
Debug.Log ("Bottle is here");
}else{
Debug.Log ("Bottle is not here");
}
if(other.gameObject.CompareTag("Bottle")){
EnteredTrigger= true;
}
}
}
Thanks a lot
Answer by cgklutts · Dec 22, 2019 at 11:37 AM
Could you not just put the collider on the game object you are carrying? If not.. you could put a bool on the player and trigger toggle it on and off if you are carrying that item.. and use logic that way within the collsion..
Hey thanks for the answer!
that's where i'm butting my head on, when i specify in the ONtriggerEnter method the collider i use, it picks the collider of my hand, and i don't know how to access "m_bottle" box collider to use it to trigger the function.
i wanted to use use the tag of the gameobject i have but it doesn't seem to work.
So when you play this game the console prints out "Bottle is not here" correct?
You shouldn't need //m_bottle = GameObject.FindGameObjectWithTag("Bottle"); This code at all. If you are carrying a game object that has a collider and a rigid body attatched to it.. and both are set to trigger it should pick up the collision within the OnTriggerEnter..
after you Instantiate the game object you are going to need to set EnteredTrigger to false or you are going to spawn a zillion of them.
Your answer
Follow this Question
Related Questions
Ball activating triggers even with a script 0 Answers
Weird behaviour with OnTriggerEnter 0 Answers
Can't click gameobject when over another trigger? 1 Answer
My script isn't detecting OnTriggerEnter why? 1 Answer
Undefind tag? 0 Answers