Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Issues with Raycasting and Throw Pillows

Discussion in 'Physics' started by KhoolWasHereIsPog, Jan 9, 2024.

  1. KhoolWasHereIsPog

    KhoolWasHereIsPog

    Joined:
    Apr 3, 2023
    Posts:
    16
    Just a note, throw pillows barley matter, that is just bad comedy.

    I have a script to highlight game objects with a selectable tag, and the length is set to 5 units. The raycast works with every object with a selectable tag except for a throw pillow. The pillow has a mesh collider, mesh renderer, and has the tag selectable. Here is my script, an image showing the raycast intersecting, and an image showing the inspector tab for my pillow.

    Also, if anyone has suggestions on how to better format pictures on this forum, please let me know.

    upload_2024-1-9_9-32-32.png

    upload_2024-1-9_9-33-30.png


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using Unity.VisualScripting;
    5. using UnityEngine;
    6.  
    7. public class Select : MonoBehaviour
    8. {
    9.     [SerializeField] private string selectableTag = "Selectable";
    10.     [SerializeField] private Material HighlightMat;
    11.     public int Lentgh;
    12.     private Material NormalMat;
    13.     public Camera cam;
    14.  
    15.     private Transform _selection;
    16.  
    17.     void Update()
    18.     {
    19.         // Deselect object
    20.         if (_selection != null) // see if the selection is null or not
    21.         {
    22.             var selectionRender = _selection.GetComponent<Renderer>(); // get renderer component
    23.             selectionRender.material = NormalMat;
    24.             _selection = null;
    25.         }
    26.  
    27.         Vector3 point = new Vector3(cam.pixelWidth / 2, cam.pixelHeight / 2, 0);
    28.         var ray = Camera.main.ScreenPointToRay(point);
    29.         RaycastHit hit;
    30.         if (Physics.Raycast(ray, out hit, Lentgh)) // Change the raycast length to Lentgh
    31.         {
    32.             Debug.DrawRay(ray.origin, ray.direction * Lentgh, Color.yellow); // Draw the ray in the Scene view
    33.  
    34.             var selection = hit.transform; // find what object was hit
    35.             if (selection.CompareTag(selectableTag)) // is it selectable?
    36.             {
    37.                 var selectionRender = selection.GetComponent<Renderer>(); // get renderer component
    38.                 NormalMat = selectionRender.material;
    39.                 if (selectionRender != null)
    40.                 {
    41.                     selectionRender.material = HighlightMat; //highlight it with the material
    42.                     if (Input.GetMouseButtonDown(0))
    43.                     {
    44.                         Debug.Log("Maybe this wont suck?");
    45.                     }
    46.                 }
    47.                 _selection = selection;
    48.             }
    49.         }
    50.     }
    51.  
    52. }
    53.  
    54.  
    55.  
    56.  
    57.  
     
  2. KhoolWasHereIsPog

    KhoolWasHereIsPog

    Joined:
    Apr 3, 2023
    Posts:
    16
    Just realized I should let you know that the material should be a transparent dark red.
     
  3. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    114
    Sorry if I've misunderstood, it's not clear to me from the image what the ray is hitting.
    I think if you
    Code (CSharp):
    1. Debug.DrawRay(ray.origin, ray.direction * hit.distance, Color.yellow);
    I'd be able to see what it's hitting.
    I think I'd put breakpoints or Debug.Log statements everywhere and try and figure out what's going on, like how deep into the if statements is it getting:)
     
  4. KhoolWasHereIsPog

    KhoolWasHereIsPog

    Joined:
    Apr 3, 2023
    Posts:
    16
    Yea, that is a good idea, especially if I put debug.log statements with random numbers, so I can differentiate between the collisions. Also, in the upper part of the first image there is a yellow line. That is the raycast.