Hello, I am currently creating an app where the user can draw on the screen after pressing a button. I also wanted to have an erase function where once the user is done drawing they can click another button to now erase whatever drawing they have done. I am new to unity and stuck on how I can create this eraser function to delete the drawings. I use a trail rendered to create the drawings and they become clones of a prefab. What do I do to delete these clones?
Here is the code for the drawing–
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Draw : MonoBehaviour
{
public GameObject drawPreFab;
GameObject theTrail;
Plane planeObject;
Vector3 startPos;
bool dr = false;
// Start is called before the first frame update
void Start()
{
planeObject = new Plane(Camera.main.transform.forward * -1, this.transform.position);
}
// Update is called once per frame
void Update()
{
dr = ClickExample.testBool;
if (dr == true)
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began || Input.GetMouseButtonDown(0))
{
theTrail = (GameObject)Instantiate(drawPreFab, this.transform.position, Quaternion.identity);
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float _dis;
if (planeObject.Raycast(mouseRay, out _dis))
{
startPos = mouseRay.GetPoint(_dis);
}
}
else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetMouseButton(0))
{
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float _dis;
if (planeObject.Raycast(mouseRay, out _dis))
{
theTrail.transform.position = mouseRay.GetPoint(_dis);
}
}
}
}
}
,I am trying to make and app where once a button is pushed you can be able to ‘erase’ away what has been drawn on the screen. I’m very new to unity and am having trouble removing the prefab clones that are created when drawing on the screen. I am using a Trail renderer to create the prefab clones. Is there anyway to delete these prefab clones when the user clicks/touches over the clones?
Here is the code –
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Draw : MonoBehaviour
{
public GameObject drawPreFab;
GameObject theTrail;
Plane planeObject;
Vector3 startPos;
bool dr = false;
// Start is called before the first frame update
void Start()
{
planeObject = new Plane(Camera.main.transform.forward * -1, this.transform.position);
}
// Update is called once per frame
void Update()
{
dr = ClickExample.testBool;
if (dr == true)
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began || Input.GetMouseButtonDown(0))
{
theTrail = (GameObject)Instantiate(drawPreFab, this.transform.position, Quaternion.identity);
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float _dis;
if (planeObject.Raycast(mouseRay, out _dis))
{
startPos = mouseRay.GetPoint(_dis);
}
}
else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetMouseButton(0))
{
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float _dis;
if (planeObject.Raycast(mouseRay, out _dis))
{
theTrail.transform.position = mouseRay.GetPoint(_dis);
}
}
}
}
}