- Home /
Question by
NaveGCT · Sep 16, 2020 at 07:55 PM ·
scripting problemscript errorlaser beamdissapearflashing
Object flashing in and out every frame (because of my script)
My object keeps flashing in and out of existance. Im trying to make a 3d laser beam (no line renderer) Here is the script it uses: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class laser : MonoBehaviour
{
private LineRenderer lr;
public Vector3 reachpoint;
public GameObject player;
public GameObject beam;
public float distance;
public GameObject ReachPointG;
public bool hittingobject = false;
public Vector3 oldpos;
public Vector3 newpos;
public Quaternion oldrot;
public Quaternion newrot;
void Start()
{
lr = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
//UpdatePos();
RaycastHit hit;
if (Physics.Raycast(player.transform.position, player.transform.forward, out hit))
{
if (hit.collider)
{
reachpoint = hit.point;
//player.transform.LookAt(reachpoint);
distance = Vector3.Distance(player.transform.position, reachpoint);
beam.transform.localScale = new Vector3(1, distance / 2, 1);
float midxp = player.transform.position.x;
float midxr = reachpoint.x;
float midx = ((midxp - midxr) / 2) + midxr;
float midyp = player.transform.position.y;
float midyr = reachpoint.y;
float midy = ((midyp - midyr) / 2) + midyr;
float midzp = player.transform.position.z;
float midzr = reachpoint.z;
float midz = ((midzp - midzr) / 2) + midzr;
beam.transform.position = new Vector3(midx, midy, midz);
}
}
else
{
Vector3 rp = new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z);
reachpoint = rp + (player.transform.forward*5000);
}
}
}
Comment