Cant Kill enemy with Raycast C# (SOLVED)
Pretty much title says, and also the HP of the enemy don't update at all, I'm super tired and frustrated so it's hard to be polite too, sorry for that, here's the shooting script:
using UnityEngine;
using System.Collections;
public class shootScript : MonoBehaviour {
public enemyScript other;
// Use this for initialization
void Start () {
other = GetComponent<enemyScript>();
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetButtonUp("Fire1"))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100) && gameObject.CompareTag("enemy"))
{
other.HP-=95;
}
}
}
}
Enemy script:
using UnityEngine;
using System.Collections;
public class enemyScript : MonoBehaviour {
public int HP = 100;
public void dead()
{
if (HP == 0)
{
DestroyImmediate(gameObject);
}
}
public void isHit()
{
HP -= 95;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (HP == 0)
{
dead();
}
}
}
thanks I'll change that, but the health of the enemy dosent update at all.
Alright I see another problem at line 20. You're comparing the "enemy" tag with the shootScript object ins$$anonymous$$d of the enemy object. It should work now if you change it to this:
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.collider.CompareTag("enemy"))
{
other.HP-=95;
}
}
Your answer
Follow this Question
Related Questions
Any way to increase raycast range? 0 Answers
Airstrike in a area around the player 1 Answer
How can I make my Raycast ignore my player, but have other player's Raycasts be able to hit it? 1 Answer
Can't make my AI shoot projectiles with raycast 0 Answers
how to check if an object is betwen enemy and player 1 Answer