Search Unity

Raycasting cannot hit small objects?

Discussion in 'Scripting' started by csgoalaskan, Jun 7, 2018.

  1. csgoalaskan

    csgoalaskan

    Joined:
    Jun 5, 2018
    Posts:
    8
    Im working on picking up items with a raycast script is below, so the problem is how it sounds i can't pick up/detect smaller items with my raycasting setup anything smaller than the default cube and it has a hard time.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class RaycastManager : MonoBehaviour
    6. {
    7.     public GameObject underline;
    8.     private GameObject raycastedObj;
    9. public GameObject rootItem;
    10.     public GameObject inventory;
    11.     public GameObject itemDatabase;
    12.     private Transform[] slot;
    13.     public GameObject slotHolder;
    14.     [Header("Raycast Settings")]
    15.     [SerializeField] private float rayLength = 0.25f;
    16.     [SerializeField] private LayerMask newLayerMask;
    17.     [Header("References")]
    18.     [SerializeField] private Image crossHair;
    19.     [SerializeField] private Text itemNameText;
    20.     [SerializeField] private playerVitals playerVitals;
    21.  
    22. public void Start() {
    23.    
    24.      GetAllSlots();
    25.    
    26.    
    27.    
    28.    
    29. }
    30.   public void Update()
    31.     {
    32.         RaycastHit hit;
    33.         Vector3 fwd = transform.TransformDirection(Vector3.forward);
    34.  
    35.         if (Physics.Raycast(transform.position, fwd, out hit, rayLength, newLayerMask.value))
    36.         {
    37.             if (hit.collider.CompareTag("Consumable"))
    38.             {
    39.                 CrosshairActive();
    40.                 raycastedObj = hit.collider.gameObject;
    41.                 ItemProperties properties = raycastedObj.GetComponent<ItemProperties>();
    42.                 itemNameText.text = properties.itemName;
    43.                 underline.SetActive(true);
    44.          
    45.                 if (Input.GetMouseButtonDown(0))
    46.                 {
    47.                     properties.Interaction(playerVitals);
    48.                 }
    49.             }
    50.  
    51.      
    52.         }
    53.      
    54.  
    55.         else
    56.         {
    57.             CrosshairNormal();
    58.             itemNameText.text = null;
    59.                 underline.SetActive(false);
    60.         }
    61.      
    62.          if (Physics.Raycast(transform.position, fwd, out hit, rayLength, newLayerMask.value))
    63.         {
    64.             if (hit.collider.CompareTag("Item"))
    65.             {
    66.                 CrosshairActive();
    67.                 raycastedObj = hit.collider.gameObject;
    68.                 ItemProperties properties = raycastedObj.GetComponent<ItemProperties>();
    69.                 itemNameText.text = properties.itemName;
    70.                 underline.SetActive(true);
    71.          
    72.                 if (Input.GetMouseButtonDown(0))
    73.                 {
    74.                     AddItem(gameObject);
    75.                  
    76.                 }
    77.             }
    78.  
    79.      
    80.         }
    81.      
    82.  
    83.         else
    84.         {
    85.             CrosshairNormal();
    86.             itemNameText.text = null;
    87.                 underline.SetActive(false);
    88.         }
    89.      
    90.      
    91.      
    92.     }
    93.  
    94.     void CrosshairActive()
    95.     {
    96.         crossHair.color = Color.red;
    97.     }
    98.  
    99.     void CrosshairNormal()
    100.     {
    101.         crossHair.color = Color.white;
    102.     }
    103.    
    104.     public void AddItem(GameObject rootItem) {
    105.      
    106.         //GameObject rootItem;
    107.         rootItem = raycastedObj;
    108.     for(int i = 0; i < 16; i++){
    109.         if(slot[i].GetComponent<Slot>().empty == true){
    110.          
    111.             slot[i].GetComponent<Slot>().item = rootItem;
    112.             rootItem.SetActive(false);
    113.         }
    114.      
    115.     }
    116.      
    117.     }
    118.    
    119.     public void GetAllSlots() {
    120.      
    121.      
    122.         slot = new Transform[16];
    123.         for(int i = 0; i < 16; i++)
    124.         {
    125.          
    126.             slot[i] = slotHolder.transform.GetChild(i);
    127.          
    128.         }
    129.      
    130.     }
    131.    
    132.    
    133. }

    Thanks in advance :p