- Home /
Raycast visible bullets
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);
}
}
}
Answer by sheepmccree · May 21, 2020 at 05:30 AM
You could use the LineRenderer component. It's fairly easy to use and could do what you want. Set the first position to the source, and the second position to the end of the raycast.
@sheepmccree i looked into this but all of the videos and threads i looked at i couldn't get to fit in with my script. how should i do this?
Answer by sheepmccree · May 22, 2020 at 05:30 AM
I'll write this in a separate comment, so you can see it better.
The way I would do this (of course, there could be other, better solutions, but this is what I came up with) is the following: First, create an empty Gameobject with a LineRenderer component attached to it, and make it a prefab.
Then, assign a script to it that would take care of setting the positions, and fading / dying after a time, and whatever else you would want to do. This is the script I tested with:
public class Trail : MonoBehaviour
{
public Vector3 dest;
// Start is called before the first frame update
private void Start()
{
LineRenderer lr = GetComponent<LineRenderer>();
lr.SetPosition(0, transform.position);
lr.SetPosition(1, dest);
StartCoroutine(Die());
}
private IEnumerator Die()
{
yield return new WaitForSeconds(2);
Destroy(gameObject);
}
}
Then, in your PlayerShoot script, in the Shoot method, you would instantiate it and set its destination:
Trail trail = Instantiate(bTrail, cam.transform.position, Quaternion.identity, transform).GetComponent();
trail.dest = _hit.point;
If the raycast successfully hit something, and
Trail trail = Instantiate(bTrail, cam.transform.position, Quaternion.identity, transform).GetComponent<Trail>();
trail.dest = cam.transform.forward * weapon.range;
If it didn't hit. bTrail would be the bullet trailing prefab I talked about in the beginninng. I hope this will suffice as a quick starter for you! If there's something else, I'll be happy to help.
Follow this Question
Related Questions
LineRender in Raycast 1 Answer
How can I can I cast a ray from a gameobject? 1 Answer
Raycast bullet collision problem 1 Answer
instantiate decal at raycastHit 1 Answer
Raycast not hitting instantiated object when timescale is 0 1 Answer