Объект, к которому прикреплен скрипт, уже имеет компонент Trail Renderer.
Скрипт, помеченный / * и * /, работает с Line Renderer, но при быстром перемещении мыши в некоторых местах он отсутствует рисунок, как будто есть пробелы. Поэтому я пытаюсь использовать Trail Renderer вместо этого, чтобы сделать свободный рисунок на местности, а также чтобы оставить след позади.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class PathCreator : MonoBehaviour
{
TrailRenderer Trail;
private void Awake()
{
Trail = GetComponent<TrailRenderer>();
}
void Update()
{
if (((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) || Input.GetMouseButton(0)))
{
Plane plane = new Plane(Camera.main.transform.forward * -1, this.transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance;
if (plane.Raycast(ray, out distance))
{
if (Trail == null)
{
//Trail = Instantiate(trailPrefab, ray.GetPoint(distance), Quaternion.identity);
}
else
{
Trail.transform.position = ray.GetPoint(distance);
}
}
}
else
{
if (Trail != null)
{
Trail = null;
}
}
}
/*public Action<IEnumerable<Vector3>> OnNewPathCreated = delegate { };
private LineRenderer lineRenderer;
private List<Vector3> points = new List<Vector3>();
private void Awake()
{
lineRenderer = GetComponent<LineRenderer>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
points.Clear();
}
if(Input.GetButton("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo))
{
if(DistanceToLastPoint(hitInfo.point) > 1f)
{
points.Add(hitInfo.point);
lineRenderer.positionCount = points.Count;
lineRenderer.SetPositions(points.ToArray());
}
}
}
else
{
if(Input.GetButtonUp("Fire1"))
{
OnNewPathCreated(points);
}
}
}
private float DistanceToLastPoint(Vector3 point)
{
if(!points.Any())
{
return Mathf.Infinity;
}
return Vector3.Distance(points.Last(), point);
}*/
}
Часть в / * * / используется с этим сценарием, поэтому игрок будет ходить по нарисованным линиям:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PathMover : MonoBehaviour
{
private NavMeshAgent navmeshagent;
private Queue<Vector3> pathPoints = new Queue<Vector3>();
private void Awake()
{
navmeshagent = GetComponent<NavMeshAgent>();
FindObjectOfType<PathCreator>().OnNewPathCreated += SetPoints;
}
private void SetPoints(IEnumerable<Vector3> points)
{
pathPoints = new Queue<Vector3>(points);
}
// Update is called once per frame
void Update()
{
UpdatePathing();
}
private void UpdatePathing()
{
if(ShouldSetDestination())
{
navmeshagent.SetDestination(pathPoints.Dequeue());
}
}
private bool ShouldSetDestination()
{
if(pathPoints.Count == 0)
{
return false;
}
if(navmeshagent.hasPath == false || navmeshagent.remainingDistance < 0.5f)
{
return true;
}
return false;
}
}
Я думал, что, возможно, использование рендера следов решит проблему с пропуском в рендерере линий.