BEGINNER ALERT (JUST IN CASE…)
I am making a game pretty much based on line renderers, and I’m trying to make sort of like a ribbon out of it, meaning I want to draw a line with the mouse, and when the line has an specific amount of points, I want to stop adding points to the line and start moving the line like if it was a ribon instead of a drawing.
I want to make this by deleting the start point of the line once there is a specific amount of points in the line renderer’s array. That way it will make the illusion as if it was a ribbon with a constant length, instead of a line been drawn.
I found a similar question here, but it only had one answer and it didn’t fulfilled what I wanted. His code deleted the entire line (like a trail renderer)…
here is his code in case you can modify it so that it deletes the first point every time the amount of points is greater than an specific number:
LineRenderer lineRenderer = GetComponent<LineRenderer>();
int newPositionCount = lineRenderer.positionCount - 1;
Vector3[] newPositions = new Vector3[newPositionCount];
for (int i = 0; i < newPositionCount; i++)
{
newPositions *= lineRenderer.GetPosition(i + 1);*
}
lineRenderer.SetPositions(newPositions);
I’m not sure if I expressed myself correctly, any help would be appreciated.
Thank you!
Try this out:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(LineRenderer))]
public class Ribbon : MonoBehaviour
{
// if the distance between mouse position and previous point is higher than this value then the segment will be drawn or the ribbon will be moved
public float distance = 0.05f;
// the amount of points the ribbon has
public int ribbonLength = 20;
LineRenderer lr;
List<Vector3> position = new List<Vector3>();
float sqrMagnitude;
bool mouseDown, draw;
void Awake()
{
lr = GetComponent<LineRenderer>();
lr.startWidth = lr.endWidth = 0.1f;
lr.material = new Material(Shader.Find("Sprites/Default"));
}
void Update()
{
if (Input.GetMouseButtonDown(0))
StartDrawing();
else if (Input.GetMouseButtonUp(0))
StopDrawing();
if (mouseDown)
{
DrawUpdate();
RibbonUpdate();
Render();
}
}
void StartDrawing()
{
mouseDown = true;
draw = true;
position.Clear();
position.Add(GetPoint(Input.mousePosition));
}
void StopDrawing()
{
mouseDown = false;
draw = false;
}
void DrawUpdate()
{
if (!draw) return;
Vector3 worldMousePosition = GetPoint(Input.mousePosition);
sqrMagnitude = (worldMousePosition - position[position.Count-1]).sqrMagnitude;
if ((sqrMagnitude > distance * distance) &&
position.Count < ribbonLength)
position.Add(worldMousePosition);
if (position.Count==ribbonLength)
draw = false;
}
void RibbonUpdate()
{
if (draw) return;
Vector3 worldMousePosition = GetPoint(Input.mousePosition);
sqrMagnitude = (worldMousePosition - position[position.Count-1]).sqrMagnitude;
if (sqrMagnitude > distance * distance)
{
for(int i = 0; i < position.Count-1; i++)
position *= position[i+1];*
-
position[position.Count-1] = (worldMousePosition);*
-
}*
-
}*
-
void Render()*
-
{*
-
lr.positionCount = position.Count;*
-
lr.SetPositions(position.ToArray());*
-
}*
-
Vector3 GetPoint(Vector3 mousePosition)*
-
=> Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, -Camera.main.transform.position.z));*
}