SHARE
TWEET

Untitled

a guest Feb 18th, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Good Afternoon Group Shinies,
  2.  
  3. Raycasting is commonly done but fairly complex. The unity component used to visually draw a line of light is called a "Line Renderer" I had hoped that there was an easy way to detect collisions using this renderer. However, it seems that this component does not support true raycasting. Although, it can be made to detect collisions, the solutions are more like workarounds and are fairly complex:
  4.  
  5. See below for what I mean:
  6.  
  7. https://answers.unity.com/questions/470943/collider-for-line-renderer.html
  8.  
  9. I suggest that "true" raycasting be performed in code and the line renderer updated from code as needed based on the rays generated from code. This may sound painful, but is not too bad in practice. I recommend looking here:
  10.  
  11. https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
  12.  
  13. Here is an example:
  14.  
  15. void Update()
  16.  
  17.     {
  18.  
  19.         // Bit shift the index of the layer (8) to get a bit mask
  20.  
  21.         int layerMask = 1 << 8;
  22.  
  23.         // This would cast rays only against colliders in layer 8.
  24.  
  25.         // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
  26.  
  27.         layerMask = ~layerMask;
  28.  
  29.         RaycastHit hit;
  30.  
  31.         // Does the ray intersect any objects excluding the player layer
  32.  
  33.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
  34.  
  35.         {
  36.  
  37.             Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
  38.  
  39.             Debug.Log("Did Hit");
  40.  
  41.         }
  42.  
  43.         else
  44.  
  45.         {
  46.  
  47.             Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
  48.  
  49.             Debug.Log("Did not Hit");
  50.  
  51.         }
  52.  
  53.     }
  54. Of note here is the following: Physics.Raycast will send out a ray from the chosen origin point to the chosen destination point. It will return true if it hits something and whatever it hits will become the variable "hit". The ray will be invisible.
  55.  
  56. To see the ray quickly, without much style,  you can use Debug.DrawRay like this example to actually draw a colored line.
  57.  
  58. I recommend adjusting the Line Renderer in code based on collisions from the above example. Please let me know on Thursday if you have any questions about the example.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top