- Home /
Question by
Atomickitten15 · May 11, 2018 at 08:32 PM ·
unity 5fpslinerendererlaser beam
LineRender: I can't seem to be able to set the start and end positions of the line
I can't seem to get the LineRenderer to render a line from the front of my gun to place where the ray cast hits. Whenever I test it, the line goes in a seemingly random place. This is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class PlayerShoot : NetworkBehaviour {
private const string PlayerTag = "Player";
[SerializeField]
GameObject EndPoint;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
[SerializeField]
LineRenderer lineRenderer;
// Use this for initialization
void Start() {
lineRenderer.enabled = false;
}
// Update is called once per frame
void Update() {
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
[Client]
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, 1000f))
{
LaserShoot(hit);
if (hit.collider.tag == PlayerTag)
{
CmdPlayerShot(hit.collider.name, 50);
}
}
}
[Command]
void CmdPlayerShot(string ID, int damage)
{
Debug.Log(ID + " has been shot");
Player player = GameManager.GetPlayer(ID);
player.TakeDamage(damage, ID);
}
void LaserShoot(RaycastHit hit)
{
lineRenderer.SetPosition(0, EndPoint.transform.position);
lineRenderer.SetPosition(1, hit.point);
lineRenderer.enabled = true;
}
}
Please help me solve this, I've been stuck for a very long time...
Comment