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

Error while using the OnTrigger method and a RaycastHit.

Discussion in 'Scripting' started by Skullman-Games, Jul 29, 2020.

  1. Skullman-Games

    Skullman-Games

    Joined:
    Jul 29, 2020
    Posts:
    2
    Hi folks, I have a problem and haven't found a solution yet. I hope you can help me. I would like to add an item to my inventory by clicking on it in my 3D game. This should only work if the player is close to the item. This should be made possible by an OnTriggerEnter and OnTriggerExit method. The method checks whether an object that triggers with the player's TriggerCollider has the tag "Selectable" and renames it "SelectableNP". The OnTriggerExit method does the opposite. If I would click on the item then the selector script takes over. This checks whether the left mouse button was pressed and uses a raycast hit to determine whether I clicked on the collider of a game object with the "SelectableNP" tag. If so, it adds the GameObject to the inventory.

    But the mistake comes in between: I can't collect the item. Actually all three methods work because I have successfully tested them all. It seems to be because I can't use both at once. At least I think so. But I'm not giving up and still hope that someone can find a solution.


    Here are the OnTriggerEnter and OnTriggerExit methods:
    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2.         {
    3.             if (other.gameObject.CompareTag("Selectable"))
    4.             {
    5.                 other.gameObject.tag = "SelectableNP";
    6.             }
    7.         }
    8.  
    9.         private void OnTriggerExit(Collider other)
    10.         {
    11.             if (other.gameObject.CompareTag("SelectableNP"))
    12.             {
    13.                 other.gameObject.tag = "Selectable";
    14.             }
    15.         }
    And here is the update method of the selector script:
    Code (CSharp):
    1. private void Update()
    2.         {
    3.             if (Input.GetMouseButtonDown(0))
    4.             {
    5.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.                 RaycastHit hit;
    7.  
    8.                 if (Physics.Raycast(ray, out hit))
    9.                 {
    10.                     if (hit.collider.gameObject.CompareTag("SelectableNP"))
    11.                     {
    12.                         IInventoryItem ItemToCollect = hit.collider.GetComponentInParent<IInventoryItem>();
    13.  
    14.                         inventory.AddItem(ItemToCollect);
    15.                     }
    16.                 }
    17.             }
    18.         }
     
  2. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    There is a much more straightforward (and probably more reliable) way to achieve what you're doing ;):

    Code (CSharp):
    1. void OnClick()
    2. {
    3.     RaycastHit hit;
    4.     Ray ray = camera.ScreenPointToRay(Input.mousePosition);
    5.    
    6.     if (Physics.Raycast(ray, out hit))
    7.     {
    8.         if(hit.transform.gameObject.tag == "Selectable" && Vector3.Distance(hit.transform.position, player.transform.position) < 2f)
    9.         {
    10.             // pickup/interact etc
    11.         }
    12.     }
    13. }
    14.  
    No need for colliders etc
     
  3. Skullman-Games

    Skullman-Games

    Joined:
    Jul 29, 2020
    Posts:
    2

    Oh. Thats it? Thank you!

    But I would still like to know why my problem is there.
     
  4. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    Could be a number of things, it's hard to tell. The code seems fine. Maybe the colliders aren't triggering like you're expecting. Maybe AddItem isn't doing what you're expecting. Maybe GetComponent is failing. If you sprinkle some Debug.Logs in there you should be able to figure it out :)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Ah yes, works every time. Source: haven't seen it fail yet! :)
     
    AnthonySharp likes this.