Question by
Henry George · May 26, 2016 at 06:44 PM ·
raycastingraycasthit
Casting a ray from empty game object but did'nt know how to get the name of game object infront of the ray ?
Here is my code of casting a ray from game object. i know it will be pretty simple but but now this time i am stuck in there. using UnityEngine;
public class castray : MonoBehaviour { void FixedUpdate() { Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 10))
Debug.DrawRay(transform.position, fwd, Color.green);
}
}
thanks in advance.
Comment
Best Answer
Answer by JedBeryll · May 26, 2016 at 07:23 PM
public class castray : MonoBehaviour {
void FixedUpdate() {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit hit;
if (Physics.Raycast(transform.position, out hit, fwd, 10)) {
Debug.DrawRay(transform.position, fwd, Color.green);
print(hit.collider.name); //prints hit gameobject's name
}
}
}
Your answer

Follow this Question
Related Questions
Help with Raycast Script 0 Answers
Getting a specific gameobject to move from Raycasthit 0 Answers
I can't get RayCast to report the collider I want. 1 Answer
Physics.Raycast Not Working... Kinda 0 Answers
Unity Raycasting Issue 1 Answer