- Home /
LineRenderer laser target not updating position
Greetings..
i have a 3d ship moving around in space from a top down view and asteroids that are also moving,
when i click on a asteroid with the mouse i want to turn on the laser and start draining the ore, if i click the same asteroid again or get out of range i want to turn off the laser,
mostly its working the laser comes out of my ship and updates its position as i orbit the asteroid,
but the problem is the end of the laser wont lock on to the asteroid, as the asteroid moves the end of the laser just stays where i first clicked.
please any help in the right direction on how to update the lasers target, thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour
{
private LineRenderer lineRenderer;
private bool toggle = false;
void Start()
{
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.enabled = false;
}
void Update()
{
lineRenderer.SetPosition(0, transform.position);
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000))
{
Debug.DrawLine(transform.position, hit.point);
if (hit.collider.tag == "Asteroid")
{
Debug.Log("Started mining!");
LaserOn();
lineRenderer.SetPosition(1, hit.point);
}
}
}
}
public void LaserOn()
{
if (!toggle)
{
lineRenderer.enabled = true;
toggle = true;
}
else
{
lineRenderer.enabled = false;
toggle = false;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612162858im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Line Renderer with sprites 0 Answers
Line Renderer works only once 0 Answers
LineRender: I can't seem to be able to set the start and end positions of the line 0 Answers
BeamWeapon Does not damage target 0 Answers
Line Renderer & Raycasting SOS !!! 1 Answer