I am fairly new to unity and trying to create a simple fps game. at the moment i have a raycast to detect if i hit an enemy, this is working fine, however you cannot see a bullet or a trail. (i know that raycasts are not visible) how could i make it so you can see a bullet or a bullet trail? would the best way to do this be to create an object that follows the path of the raycast when i shoot? if so, how do i do this, and if not what is the best way? I think these are the relevant scrips that i have:
The PlayerShoot script, which contains the raycast:
using System.Collections;
using UnityEngine;
public class PlayerShoot : MonoBehaviour
{
public PlayerWeapon weapon;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
void Start()
{
if (cam == null)
{
Debug.LogError("PlayerShoot: No camera referenced!");
this.enabled = false;
}
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit _hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask))
{
//We hit something
if (_hit.collider.tag == "ENEMY_TAG")
{
Debug.Log("Hit for " + weapon.damage);
EnemyHealth enemyHealth = _hit.transform.gameObject.GetComponent<EnemyHealth>();
enemyHealth.currentHealth -= weapon.damage;
}
}
}
}
The PlayerWeapon script which gets the range and damage:
using UnityEngine;
[System.Serializable]
public class PlayerWeapon
{
public int damage = 10;
public float range = 100f;
}
And finally the EnemyHealth script which sets how much health the enemy has:
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
void Update()
{
if(this.currentHealth <= 0)
{
Destroy(this.gameObject);
}
}
}