Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 fl1ckje · Dec 01, 2018 at 04:15 PM · scripting problemlinerendererreflectionsine-wavesine

Create a sine wave(using line renderer) and reflect it from colliders

Hello guys. I have bumped into really bad trouble. I have a line renderer which draws a line and reflects in other direction(creates a clone of that line in hit point with defined direction) if hits a collider with defined tag. So... I need to draw it not as a straight line, but as a line in sine pattern and reflect in the same way. I have no idea how to find a specific segment which would become a start point for a new(cloned) line :((( I tried a lot of ways to do it, but it failed :((( Here's my script and a picture of concept which I want to bring with a script into unity:

alt text

 using System.Collections;
 using UnityEngine;
 
 [RequireComponent(typeof(LineRenderer))]
 
 public class SignalLine : MonoBehaviour
 {
     public float updateFrequency = 0.1f;
     public float lineWidth = 1.0f;
     public int lineDist;
     public string reflectLineTag;
     public string splitTag;
     public string clonedLineTag;
     public int maxBounce;
     public int maxSplit;
     public int maxSliptPerObj;
     [Tooltip("Set Min & Max value(inclusive in range)")]
     public Vector2 randXrange, randYrange, randZrange;
     private float timer = 0;
     private LineRenderer line;
 
     private void Start()
     {
         timer = 0;
         line = gameObject.GetComponent<LineRenderer>();
         StartCoroutine(RedrawLaser());
     }
 
 
     private void Update()
     {
         if (gameObject.tag != clonedLineTag)
         {
             if (timer >= updateFrequency)
             {
                 timer = 0;
                 //Debug.Log("Redrawing line");
                 foreach (GameObject lineSplit in GameObject.FindGameObjectsWithTag(clonedLineTag))
                     Destroy(lineSplit);
                 StartCoroutine(RedrawLaser());
             }
             timer += Time.deltaTime;
         }
         else
         {
             line = gameObject.GetComponent<LineRenderer>();
             StartCoroutine(RedrawLaser());
         }
     }
 
     private IEnumerator RedrawLaser()
     {
         //Debug.Log("Running");
         int lineSplit = 1;
         int lineReflected = 1;
         int posIndex = 1; //How many line segments are there
         bool loopActive = true; //Is the reflecting loop active?
         Vector3 lineDir = transform.forward; //direction of the next line
         Vector3 lastLinePos = transform.localPosition; //origin of the next line
         line.positionCount = 1;
         line.SetPosition(0, transform.position);
         RaycastHit hit;
         while (loopActive)
         {
             //Debug.Log("Physics.Raycast(" + lastLinePos + ", " + lineDir + ", out hit , " + lineDist + ")");
             if (Physics.Raycast(lastLinePos, lineDir, out hit, lineDist) && ((hit.transform.gameObject.tag == reflectLineTag) || (hit.transform.gameObject.tag == splitTag)))
             {
                 //Debug.Log("Bounce");
                 lineReflected++;
                 posIndex += 3;
                 line.positionCount = posIndex;
                 line.SetPosition(posIndex - 3, Vector3.MoveTowards(hit.point, lastLinePos, lineWidth));
                 line.SetPosition(posIndex - 2, hit.point);
                 line.SetPosition(posIndex - 1, hit.point);
                 line.startWidth = lineWidth;
                 line.endWidth = lineWidth;
                 lastLinePos = hit.point;
                 //Vector3 prevDirection = lineDir; for other method of splitting the line
                 lineDir = Vector3.Reflect(lineDir, hit.normal);
                 if (hit.transform.gameObject.tag == splitTag)
                 {
                     //Debug.Log("Split");
                     if (lineSplit >= maxSplit)
                     {
                         //Debug.Log("Max split reached");
                     }
                     else
                     {
                         //Debug.Log("Splitting");
                         lineSplit++;
                         for (int k = 0; k < maxSliptPerObj; k++)
                         {
                             GameObject clone = Instantiate(gameObject, hit.point, Quaternion.FromToRotation(hit.point, new Vector3
                         (Random.Range(hit.normal.x + randXrange.x, hit.normal.x + randXrange.y),
                         Random.Range(hit.normal.y + randYrange.x, hit.normal.y + randYrange.y),
                         Random.Range(hit.normal.z + randZrange.x, hit.normal.z + randZrange.y))));//Use Quaternion.LookRotation(prevDirection) to send line clone through the obj
                             clone.GetComponent<LineRenderer>().startColor = Color.red;
                             clone.GetComponent<LineRenderer>().endColor = Color.red;
                             clone.name = clonedLineTag;
                             ((GameObject)clone).tag = clonedLineTag;
                         }
                     }
                 }
             }
             else
             {
                 //Debug.Log("No bounce");
                 lineReflected++;
                 posIndex++;
                 line.positionCount = posIndex;
                 //Vector3 lastPos = lastLinePos + (lineDir.normalized * lineDist);for debug
                 //Debug.Log("InitialPos " + lastLinePos + " Last Pos" + lastPos);
                 line.SetPosition(posIndex - 1, lastLinePos + (lineDir.normalized * lineDist));
                 loopActive = false;
             }
             if (lineReflected > maxBounce)
                 loopActive = false;
         }
         yield return new WaitForEndOfFrame();
     }
 }


painfulpic.png (13.9 kB)
Comment
Add comment · Show 3
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 fafase · Dec 02, 2018 at 12:42 PM 1
Share

So do you need to find a segment of the existing LineRenderer? If you have start and end point, you can find the closest one for each in the line array, iterates the collection to find closest for each them and then copy all items in between into a new array. That would be your segment.

avatar image fl1ckje fafase · Dec 02, 2018 at 06:55 PM 0
Share

Thank you! :) I have already done the method to find the point and now i try to rotate it(when it faces a reflection surface) without cloning and setting rotation of a clone.

avatar image fafase fl1ckje · Dec 03, 2018 at 05:40 PM 0
Share

Have you thought of taking each point of the result collection and then pass them one by one into Vector3.Reflect ? Then you'd get a new collection of points reflected against the plane.

0 Replies

· Add your reply
  • Sort: 

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

169 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

Related Questions

Sine movement in rotated object? 1 Answer

Line Renderer flickering when updated in runtime 2 Answers

How do you have a line renderer render multiple lines from 1 mouse click? 1 Answer

Call Update once per object vs Update all in a central script 2 Answers

How do I replace "SetVertexCount"? 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