Search Unity

Is it possible to fire a raycast after a rigidbody collider hits something?

Discussion in 'Scripting' started by Lethn, Apr 28, 2017.

  1. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    https://i.imgsafe.org/3465f1641d.png

    I wanted to ask if it were possible to have a collider after hitting a solid object to then fire a raycast out in the direction it hit. I wasn't quite sure how to explain this in English and then I remembered I could draw so I put up a diagram which hopefully makes sense to all of you.

    You may rightfully ask why I don't just use a raycast as has been suggested to me before, well I definitely already thought about that, but I'm thinking about what if I wanted to have some kind of spike weapon or something that had rigidbody physics to it's ammo, this is where I'm getting at with this idea.

    Is it possible to grab the impact direction of a rigidbody with another solid object and then use that position data to then fire off a raycast to the wall behind it? As you can see I'm thinking of the slightly fancy blood splatter mechanics again I'm still using a raycast, but it would be nice to be able to still keep some proper gravity and so on for bullet physics if possible to make the game way more believable.

    Edit: I suppose alternatively oh crap, I just may have figured it out, I could have the collider constantly firing a raycast and waiting to hit an object then have the raycast instantiate the blood through the object can't I? If I could just double check whether that's right I'd be grateful guys, I'm just not 100% sure about whether I can check for a hit when using the raycast.
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    https://docs.unity3d.com/ScriptReference/Collision-relativeVelocity.html

    It's the only clear question you asked, the rest is a little rambling and didn't make much sense.
     
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Thanks to the documentation you provided I found the perfect tutorial that explains exactly what I'm wanting.

     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Thanks to the suggestions here I've almost got the effect I want, I'm just doing a basic plane for now and will work on some proper blood textures later but there's just one problem, for some reason the raycast that fires out instantiates my planes past the wall despite them not being on the mask that the raycast can go through.

    Here's the code I've got so far, I haven't had to use LayerMasks very much yet so I could just be using them incorrectly and not realising what's going on with them at the moment.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InstantiateBlood : MonoBehaviour {
    6.  
    7.  
    8.     public GameObject bloodObject;
    9.     public LayerMask BloodSplatterMask;
    10.  
    11.  
    12.     void OnCollisionEnter ( Collision col )
    13.  
    14.     {
    15.         if (col.relativeVelocity.magnitude > 2f)
    16.  
    17.         {
    18.             Vector3 fwd = transform.TransformDirection (Vector3.forward);
    19.  
    20.             if ( Physics.Raycast ( transform.position, fwd, 5, BloodSplatterMask ) )
    21.  
    22.                 {
    23.                 Instantiate (bloodObject, fwd, transform.rotation);
    24.                     Debug.Log ("Blood instantiated");
    25.                 }
    26.         }
    27.     }
    28.  
    29.  
    30.  
    31. }
    32.  
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Seems like you're positioning the bloodObject at 1 unit ahead of whatever you're looking at with

    Vector3 fwd = transform.TransformDirection (Vector3.forward);

    and

    Instantiate (bloodObject, fwd, transform.rotation);

    You don't appear to be using the hit.point data or getting it from the raycast. Seems to me that the script is working because it's been coded to do this behaviour. You should be using the RaycastHit version of Raycast in order to get rich data from what it actually hit.
     
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Ahhh, my bad, I was wondering if that was the right way forward, forgot the most basic thing.