Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by adjm2001 · Jan 03, 2021 at 08:21 PM · linerendererline rendererline drawing

How to delete the first point of a line renderer's array until there are only an specific amount of points.

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[i] = lineRenderer.GetPosition(i + 1);
         }
 
         lineRenderer.SetPositions(newPositions);

I'm not sure if I expressed myself correctly, any help would be appreciated.

Thank you!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by yummy81 · Jan 03, 2021 at 11:09 PM

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[i] = 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));
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adjm2001 · Jan 07, 2021 at 02:39 PM 0
Share

Wow Thank you!! This is perfect!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

attach drawing line using (LineRenderer) to moving background 0 Answers

Convert Line Render to 3d Mesh with Collider 0 Answers

What's the best way to draw a 2D line WITHOUT using LineRenderer? 1 Answer

Neon / Glow effect on lines in 2d 1 Answer

Raycasting along line renderer width 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges