this is my code, how do i make it so I can see the ray and I have made a enemy script which i checked and was correct.
using System.Collections;
using UnityEngine;
public class GunScript : MonoBehaviour
{
public Transform barrel;
public float range = 0f;
public float delay = 0f;
bool fired;
bool isAuto;
public KeyCode switchFireMode;
void Update()
{
if (Input.GetButton("Fire1") && !fired && isAuto)
{
fired = true;
StartCoroutine("FireAuto");
}
if (Input.GetButtonDown("Fire1") && !fired && !isAuto)
{
fired = true;
StartCoroutine("FireSemi");
}
if(Input.GetKeyDown(switchFireMode))
{
if(!isAuto)
{
isAuto = true;
}
else
{
isAuto = false;
}
}
}
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.red);
yield return new WaitForSeconds(delay);
fired = false;
}
IEnumerator FireSemi()
{
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 null;
fired = false;
Debug.Log("we made it here");
}
}