I am making an fps and am using a raycast too shoot, obviously these are invisible so i tried using a linerenderer to see where you’re shooting. how do i make it that the line appears from the centre of the screen to where the shot has gone and then disappear after a second. this is what i have at the moment. at the moment the linerenderer is in the middle of my scene and the other point goes to where i shoot.
Script
using System.Collections;
using UnityEngine;
public class PlayerShoot : MonoBehaviour
{
public PlayerWeapon weapon;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
public LineRenderer lr;
void Start()
{
if (cam == null)
{
Debug.LogError("PlayerShoot: No camera referenced!");
this.enabled = false;
}
lr = GetComponent<LineRenderer>();
}
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)
{
Debug.Log("Hit for " + weapon.damage);
EnemyHealth enemyHealth = _hit.transform.gameObject.GetComponent<EnemyHealth>();
enemyHealth.currentHealth -= weapon.damage;
lr.SetPosition(1, _hit.point);
}
}
}
}