Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- public class Draw : MonoBehaviour {
- public int distanceFromCamera;
- public GameObject drawPrefab;
- private GameObject _clonedPrefab;
- private LineRenderer _lineRender;
- private List<Vector3> _positions = new List<Vector3>();
- private void Update() {
- if (Input.GetMouseButtonDown(0)) {
- DrawLineStart();
- } else if (Input.GetMouseButton(0)) {
- Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- if (Vector3.Distance(newPosition, _positions[_positions.Count - 1]) > .1f) {
- DrawLineUpdate(newPosition);
- }
- }
- }
- private void DrawLineStart() {
- _clonedPrefab = Instantiate(drawPrefab, Vector3.zero, Quaternion.identity) as GameObject;
- _lineRender = _clonedPrefab.GetComponent<LineRenderer>();
- _positions.Clear();
- _positions.Add(fixOffset(Camera.main.ScreenToWorldPoint(Input.mousePosition)));
- _positions.Add(fixOffset(Camera.main.ScreenToWorldPoint(Input.mousePosition)));
- _lineRender.SetPosition(0, _positions[0]);
- _lineRender.SetPosition(1, _positions[1]);
- }
- private void DrawLineUpdate(Vector3 _newPosition) {
- _positions.Add(fixOffset(_newPosition));
- _lineRender.positionCount++;
- _lineRender.SetPosition(_lineRender.positionCount - 1, _positions[_positions.Count-1]);
- }
- private Vector3 fixOffset(Vector3 _newPosition) {
- return new Vector3(_newPosition.x, _newPosition.y, distanceFromCamera);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement