- Home /
Question by
Evolution-Games · Jun 03, 2017 at 11:31 AM ·
enemyshootingraycastingdamage
Shooting and damage with raycast doesnt work.
so i looked at quite a lot of tutorials and other posts regarding raycast shooting and damage but i still cant get it to work.I don't get any errors but it still doesn't work! here is my script for my gun:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunScript : MonoBehaviour
{
float bulletDamage = 10;
float range = 100f;
public Transform shootPoint;
public float fireRate = 0.1f;
float fireTimer;
void Fire()
{
if (fireTimer < fireRate) return;
RaycastHit hit;
if (Physics.Raycast(shootPoint.position, shootPoint.transform.forward, out hit, range))
{
if(hit.collider.gameObject.tag == "Enemy")
{
Debug.Log(hit.transform.name + "hit");
GameObject Enemy = hit.collider.gameObject;
Enemy.SendMessage("applyDamage", bulletDamage, SendMessageOptions.DontRequireReceiver);
}
}
fireTimer = 0.0f;
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1"))
{
Fire();
}
if (fireTimer < fireRate)
fireTimer += Time.deltaTime;
}
}
and here is my code for the enemy:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public int enemyHealth = 100;
// Use this for initialization
void Start () {
enemyHealth = 100;
}
// Update is called once per frame
void Update () {
}
}
Comment
Have you made sure that any enemy objects have their tag set to "Enemy"?