- Home /
This question was
closed May 27, 2020 at 05:34 AM by
Symbie.
Question by
Symbie · May 22, 2020 at 02:54 AM ·
raycastpositionlinerenderer
Setting the position of linerenderer
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);
}
}
}
}
Comment
Follow this Question
Related Questions
Line renderer positioning 2 Answers
Help with LineRenderer 1 Answer
Bouncing LineRenderer, null exception. 0 Answers
Line Renderer & Raycasting SOS !!! 1 Answer
Dynamically adjusting LineRenderer vertexes to follow raycasts 0 Answers