Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Get Component in RaycastHit game object

$
0
0
Im making a game in which the player (a sphere) shoots laser beams via Raycast at enemies (cubes). I want to find the "EnemyHealth.cs" component in the object that it hits and if it has the component, to trigger a bool named "isHit" Here is my code... EnemyHealth.cs using UnityEngine; using System.Collections; public class EnemyHealth : MonoBehaviour { public int health; public bool isHit; void Update () { if(isHit) { health--; } if(health <= 0) { Destroy(gameObject); } } } Attack.cs using UnityEngine; using System.Collections; public class Attack : MonoBehaviour { public float range = 0f; private GameObject enemyTargeted; void Update () { if(Input.GetButtonDown("Fire1")) { RaycastHit hit; Ray ray = new Ray(transform.position, Input.mousePosition); if(Physics.Raycast(ray, out hit, range)) { if(hit.collider.tag == "Enemy") { hit.gameObject.GetComponent().isHit = true; } } } } } Thanks!

Viewing all articles
Browse latest Browse all 1488

Trending Articles