- Home /
destroy object by using line renderer
Wanted to ask that how to destroy an object by using line renderer.
every time i swipe to the object i spawn, the line just behind the object and when i set ontriggerenter to them, the object wont be destroy.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LineFollow : MonoBehaviour {
int vertexcount;
public bool mousedown = false;
private LineRenderer line;
// Use this for initialization
void Start () {
line = GetComponent<LineRenderer> ();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
mousedown = true;
}
if (mousedown) {
line.positionCount = vertexcount + 1;
//line.SetVertexCount (vertexcount + 1);
Vector3 mousepos = Camera.main.ScreenToWorldPoint(new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 15f));
line.SetPosition (vertexcount, mousepos);
vertexcount++;
BoxCollider collider = gameObject.AddComponent<BoxCollider> ();
collider.transform.position = line.transform.position;
collider.size = new Vector3 (0.2f, 0.2f);
}
if (Input.GetMouseButtonUp (0)) {
vertexcount = 0;
line.positionCount = 0;
//line.SetVertexCount (0);
mousedown = false;
DestroyCollider ();
}
}
private void DestroyCollider()
{
BoxCollider[] box = GameObject.FindObjectsOfType<BoxCollider> ();
foreach (BoxCollider _box in box) {
Destroy (_box);
}
}
void OnTriggerEnter(Collider col)
{
if (col.tag == "mons") {
Destroy (col.gameObject);
}
}
}
code updated, i add a collider inside the line renderer, but when i swipe to the object, nothing happen.
I don't understand. Are you trying to use the line renderer as collider to detect the objects?
@Panga$$anonymous$$i yes, i want to use the line renderer as collider to detect the object in order to destroy the object.
Answer by Pangamini · Mar 07, 2018 at 09:19 AM
LineRenderer (or any renderer) is NOT a collider. It won't ever send any messages related to physics. If you want to detect a swipe as a physical object, I'd suggest using one or more raycasts (or sphere casts if you want your line to be 'thicker' physically).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Throw with mouse 1 Answer
Mouse Swipe and Code Efficiency 0 Answers
How to move an object using mouse just like the swipe in iOS? 1 Answer
Distribute terrain in zones 3 Answers