Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I only grab objects within reach?

Discussion in 'Scripting' started by BladeManEXE10, Jul 29, 2019.

  1. BladeManEXE10

    BladeManEXE10

    Joined:
    Mar 21, 2019
    Posts:
    20
    After some Youtube tutorials by Sykoo, along with some of my own tweaks, I have an inventory system and items you can pick up by right clicking on them.

    In fact, there's already a system for detecting if the player is within reach of the item.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Item : MonoBehaviour
    6. {
    7.     public int ID;
    8.     public string type;
    9.     public string description;
    10.     public Sprite icon;
    11.  
    12.     [HideInInspector]
    13.     public bool equipped;
    14.  
    15.     [HideInInspector]
    16.     public GameObject weapon;
    17.  
    18.     [HideInInspector]
    19.     public GameObject weaponManager;
    20.  
    21.     public bool playersWeapon;
    22.  
    23.     public Inventory inventory;
    24.  
    25.     public GameObject player;
    26.  
    27.     public bool withinReach;
    28.  
    29.  
    30.     public void Start()
    31.     {
    32.         inventory = GameObject.Find("Player").GetComponent<Inventory>();
    33.         player = GameObject.Find("Player");
    34.  
    35.         weaponManager = GameObject.FindWithTag("WeaponManager");
    36.  
    37.         if(!playersWeapon)
    38.         {
    39.             int allWeapons = weaponManager.transform.childCount;
    40.             for (int i = 0; i < allWeapons; i++)
    41.             {
    42.                 if(weaponManager.transform.GetChild(i).gameObject.GetComponent<Item>().ID == ID)
    43.                 {
    44.                     weapon = weaponManager.transform.GetChild(i).gameObject;
    45.                 }
    46.             }
    47.         }
    48.     }
    49.  
    50.     private void OnTriggerEnter(Collider player)
    51.     {
    52.         withinReach = true;
    53.     }
    54.  
    55.     private void OnTriggerExit(Collider player)
    56.     {
    57.         withinReach = false;
    58.     }
    59.  
    60.     public void OnMouseOver()
    61.     {
    62.         if (withinReach && Input.GetMouseButtonDown(1))
    63.         {
    64.             inventory.AddItem(this.gameObject, ID, type, description, icon);
    65.         }
    66.     }
    67.  
    68.  
    69.     public void Update()
    70.     {
    71.         if (equipped)
    72.         {
    73.             if (Input.GetKeyDown(KeyCode.G))
    74.             {
    75.                 equipped = false;
    76.                 this.gameObject.SetActive(false);
    77.             }
    78.         }
    79.     }
    80.  
    81.     public void ItemUsage()
    82.     {
    83.         //weapon
    84.  
    85.         if(type == "Weapon")
    86.         {
    87.             weapon.SetActive(true);
    88.             weapon.GetComponent<Item>().equipped = true;
    89.         }
    90.  
    91.         //health item
    92.  
    93.         //beverage
    94.  
    95.     }
    96.  
    97. }
    The problem is that I have a normal box collider for actually clicking on the item, and a sphere collider set as a trigger to detect if the player is within reach of the item.

    But it simply uses the sphere collider for everything and ignores the box collider. When I shoot near the item, the debugger tells me I'm hitting the item because the ray is hitting the sphere collider. And it can also make it tricky to actually pick up the item by clicking on it.

    If I move the sphere collider further down the list in the inspector, it just moves it back up when I start the game.

    Is there any way I can remove the sphere collider and use Physics.OverlapSphere instead? Or something like that?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  3. BladeManEXE10

    BladeManEXE10

    Joined:
    Mar 21, 2019
    Posts:
    20
    Glad to know this exists. Thank you.

    Although, in a hurry and without realizing it at first, I found a different solution. I just put the sphere collider on an empty object and then found I could put said object on an "Ignore Raycast" layer.