Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Walemax · Dec 18, 2020 at 02:07 PM · scripting problemscript.unity 2dmultipleline renderer

Creating multiple lines using multiple Line Renders, but all are receiving the same update.

My goal: to create multiple lines and placing them onto a grid, create random noise to change the length and direction of each line.


Basically I am trying to recreate "Drift" macOS screensaver, or called Anemona.

I think the problem is from these lines of code:

     Lines.Add(new Line(
                          new Vector2(x,y),
                          new Vector2(x,y+1),
                          counterX+counterY
                          ));
     
     -------------------------------------------------------
 for (int index = 0; index < Lines.Count; index++)
              {
                  Vector2 location = Lines[index].getLocation();
                  //noiseP = Mathf.PerlinNoise(location.x + 0.1f * random, location.y + 0.1f * random);
                  Lines[index].updateEnd(new Vector2(location.x + random, location.y + random));
              }
 



A picture of what is not supposed to happen: alt text

A description of what should happen: Each line will have a random direction.


Line.cs class:

 using UnityEngine;
 public class Line
 {
     private Vector2 start;
     private Vector2 end;
     private Color startColor;
     private Color endColor;
     GameObject myLine;
     LineRenderer lr;
 
     public Line(Vector2 start, Vector2 end, float position)
     {
         myLine = new GameObject();
         myLine.name = "Line " + position;
 
         myLine.transform.position = start;
         myLine.AddComponent<LineRenderer>();
         lr = myLine.GetComponent<LineRenderer>();
 
         lr.material = new Material(Shader.Find("Sprites/Default"));
         lr.startWidth = 0.05f;
         lr.endWidth = 0.05f;
         lr.SetPosition(0, start);
         this.start = start;
         lr.SetPosition(1, end);
         this.end = end;
 
     }
 
     public void updateEnd(Vector2 end)
     {
         this.end = end;
         lr.startWidth = 0.05f;
         lr.endWidth = 0.05f;
         lr.SetPosition(1, end);
     }
 
     public void updateLength(float ratio)
     {
         float dx = end.x - start.x;
         float dy = end.y - start.y;
         float mag = (dx * dx) - (dy * dy);
 
         Vector2 endR = new Vector2(
             ((end.x * ratio) * dx / mag),
             ((end.y * ratio) * dx / mag)
             );
 
         lr.SetPosition(1, endR);
     }
 
     public Vector2 getLocation()
     {
         return end;
     }
 
 }
 

Main line engine:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LineCreator : MonoBehaviour
 {
     List<Line> Lines = new List<Line>();
     public int width = 20;
     public int height = 20;
     public int dpi;
 
 
     // Start is called before the first frame update
     void Start()
     {
         float counterX = 0;
         float counterY = 0;
 
         for (int x = 0; x < width; x++)
         {
             counterY = 0;
             for (int y = 0; y < width; y++)
             {
                 Lines.Add(new Line(
                     new Vector2(x,y),
                     new Vector2(x,y+1),
                     counterX+counterY
                     ));
 
                 counterY += 0.1f;
             }
             counterX += 1f;
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         float noiseP;
 
         if (Input.GetMouseButtonDown(1))
         {
             float random = Random.Range(-1f, 1f);
 
             for (int index = 0; index < Lines.Count; index++)
             {
                 Vector2 location = Lines[index].getLocation();
                 //noiseP = Mathf.PerlinNoise(location.x + 0.1f * random, location.y + 0.1f * random);
                 Lines[index].updateEnd(new Vector2(location.x + random, location.y + random));
             }
         }
     }
 }
 


18-302.png (4.1 kB)
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
0

Answer by Jaxcap · Dec 18, 2020 at 02:20 PM

I didn't look into the math part of it, but just taking a look at the Update function, I feel like you're using the same random value for every Line in that List. Maybe if you regenerate the random number every time you run the loop, it'll fix it?

              float random;
  
              for (int index = 0; index < Lines.Count; index++)
              {
                  random = Random.Range(-1f, 1f);
                  Vector2 location = Lines[index].getLocation();
                  //noiseP = Mathf.PerlinNoise(location.x + 0.1f * random, location.y + 0.1f * random);
                  Lines[index].updateEnd(new Vector2(location.x + random, location.y + random));
              }
Comment
Add comment · 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

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

221 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 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

How can i change position of my character 0 Answers

I'm trying to make scorezones but i cant seems to get it right.,Im trying to make a scorezone that you can go through and that can add 1 point to the player. 0 Answers

Enemy health problem unity2D 1 Answer

How can I color array of gameobjects over time ? 2 Answers

Multiple coroutines starting from Update() freeze/crash Unity 0 Answers


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