- Home /
How do I use Raycasting to return gameobject
I need a ray to hit a gameobject, and deal damage or destroy gameobject.
This is what I have:
if(Physics.Raycast(myRay, out hit)){
Debug.Log(hit);
}
I get UnityEngine.RaycastHit returned from debug, how to take this info and use it?
Answer by maccabbe · Mar 23, 2015 at 06:26 AM
you can use hit.transform.gameObject or hit.collider.gameObject
Answer by Socrates · Mar 23, 2015 at 06:51 AM
The Unity Learn tutorial Survival Shooter found here does exactly this and may be helpful to you. Their code calls an EnemyHealth script on the enemy, which receives the damage and reduces hit points and destroys the enemy if necessary.
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent
();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
}
Your answer

Follow this Question
Related Questions
raycast help need 2 Answers
Push object in direction of camera 0 Answers
Raycast not colliding with selected layer 0 Answers
Accessing Variables from Another Script 1 Answer
General Way To 'Widen' A Ray Cast? 1 Answer