Question by
DarksoulUnion · Feb 02, 2017 at 02:29 PM ·
c#
Raycast doesn't work No error report
Hey I have A 3D Weapon and a working Third Person Script. My Problem: It happens nothing even if both Scripts are enabled The Gun script and Enemy Script Fire1 is Set in Input. Code for Gun Script: using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class gunscript : MonoBehaviour { public Transform barrel; public float range = 50f;
public float delay = 0f;
bool fired;
private void Update()
{
if (Input.GetButton("Fire1")&& !fired)
{
fired = true;
StartCoroutine("Fire");
}
}
IEnumerator Fire()
{
RaycastHit hit;
Ray ray = new Ray(barrel.position, transform.forward);
if (Physics.Raycast(ray, out hit, range))
{
if(hit.collider.tag == "Enemy")
{
Enemy enemy = hit.collider.GetComponent<Enemy>();
enemy.health -= 1;
}
}
Debug.DrawRay(barrel.position, transform.forward * range, Color.green);
yield return new WaitForSeconds(delay);
fired = false;
}
} Enemy Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public int health;
private void Update()
{
Debug.Log(health.ToString());
if (health <= 0)
{
Destroy(gameObject);
}
}
}
Comment