- Home /
 
How to access component that the raycast hit
I'm basically wondering how to access a rigid body from the bone that gets hit by the raycast, that way I can apply force to it once it gets shot. How would I implement this in code?
               Comment
              
 
               
              Answer by Kaskorian · May 27, 2018 at 02:23 PM
Access rigid body from raycast target Example:
 RaycastHit hit;
 
 Physics.Raycast(new Ray(Vector3.zero, Vector3.forward), out hit);
 
 Rigidbody rg;
 
 if (hit.collider.gameObject.GetComponent<Rigidbody>() != null)
     rg = hit.collider.gameObject.GetComponent<Rigidbody>();
 
 
              Your answer
 
             Follow this Question
Related Questions
change to raycast shooting instead of rigidbody shooting 1 Answer
Get hit direction from one gameObject hitting another 2 Answers
Best way to shoot physical bullets? 2 Answers
Hovercraft Physics Problems 1 Answer
Recoil on self-object 1 Answer