- Home /
How do i get the transform of an object with raycasthit?
I want to get the transform of the object i hit and then instantiate anoter above.
You can find your answer here: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Answer by fafase · Feb 18, 2018 at 11:23 AM
Unity does not provide a straightforward way to grab a game object out of a raycast hit. The RaycastHit object contains a reference to the Collider that was hit. That collider is the component and like any component, it has a reference to the transform of the game object.
public class RaycastExample : MonoBehaviour
{
void FixedUpdate()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit))
print("Found an object : " + hit..collider.transform);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612152847im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unable to apply "transform" on gameobject from Raycast 1 Answer
C# Raycast goes straight into the air 2 Answers
NullReferenceException when using raycast 1 Answer
Fixing Editor Mouse Offset Due To Local Raycast? 1 Answer
How do I change movement speed of my Raycasting A*pathfinding transform? 2 Answers