- Home /
Using Raycasting For Enemies Attacking Player Objects
Hello people,
I will have a fair few enemies on the stage at one time and they will be attacking buildings/turrets placed by the player. Each building/turret will have x amount of health and the enemies will have to destroy these.
At the moment, we have an actual object being fired at the buildings and turrets, this works until you have more than one placed (as we're using name/tag) because it just depletes the health of all objects with that name or tag. What is the best way for the enemies to do damage to the buildings and turrets? I assume raycasting but I'm not really that great with it.
To further this question. How would I access the health(script) of the object the enemy is shooting at?
Answer by speedything · Aug 05, 2012 at 08:30 PM
If you're already using objects being fired your best bet is to use OnCollisionEnter.
void OnCollisionEnter(Collision collision)
{
GameObject hitObject = collision.gameObject;
hitObject.GetComponent<Health_Script>(); // Do what you need with this.
}
This should be attacked to the projectile, and make sure it has a rigidbody.
Your answer

Follow this Question
Related Questions
Need Help with 2D raycast 0 Answers
Raycasting and enemies ai 2 Answers
Click Point From Raycasting Not Yielding Expected Coordinates 1 Answer
SInce when?!?!? (Raycast issue) 2 Answers
Checking the position of the mouse cursor in 3d space 2 Answers