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 wyatts · Feb 01, 2017 at 09:07 AM · collisionvector3linerendererreflection

Basic LineRender Reflection

Hello! I'm just starting out and trying to get a line using the Line Renderer to reflect off of an object. I've looked all over and there's some added confusion as SetVertexCount has been deprecated.

Does anyone know how you would take the Vector3.Reflect example and get it working with a Line Renderer?


 public class ExampleClass : MonoBehaviour {
     public Transform originalObject;
     public Transform reflectedObject;
     void Update() {
         reflectedObject.position = Vector3.Reflect(originalObject.position, Vector3.right);
     }
 }

It might not be worth looking at, but here is my attempt:

 public class RaycastTest : MonoBehaviour {

 public float maxRayDistance = 1.0e30f;
 private LineRenderer line;

 void Start () {
     line = GetComponent<LineRenderer> ();
 }

 void FixedUpdate () {
     Laser ();

     //Debug line for raycast reference
     Debug.DrawLine (transform.position, transform.position + Vector3.up * maxRayDistance, Color.red);

 }
 void Laser () {
     Ray ray = new Ray (transform.position, Vector3.up);
     RaycastHit hit;
     line.SetPosition (0, ray.origin);

     if (Physics.Raycast (ray, out hit, 100)) {
         line.SetPosition (1, hit.point);
     } else {
         line.SetPosition (1, ray.GetPoint (100));
     }

     if (hit.collider.tag == "Mirrors") {
         line.SetPosition (2, hit.point + 100.0f * Vector3.Reflect (transform.forward, hit.normal));
     }
 }
 }


Also if anyone has an idea of where to look for more resources on this type of thing it would be greatly appreciated! Thanks!

EDIT: So I realized my code actually sort of works once you go into the Line Renderer properties and make the Positions Size "3"

Now the issue seems to be the reflect direction is pretty far off

video of bad reflection

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

2 Replies

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

Answer by wyatts · Feb 06, 2017 at 09:16 PM

So the weird reflection was actually due to the 100.0f I multiplied to the Vector3.Reflect (not sure why I did that!) -- but remove it and everything works great.

Comment
Add comment · Show 4 · 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 TheRockRipper · Jun 22, 2017 at 06:01 AM 0
Share

I'm trying to make the Line Renderer reflect multiple times on any number of objects that appear in the path of the rendered line. I'm trying to do it by increasing the number of vertices. But it limits the number of times I can replicate the behaviour and increases the length of the script. Is there a way to do it using Loops and Arrays?

avatar image wyatts · Jun 22, 2017 at 06:11 AM 0
Share

I know that the SetVertexCount thing has been deprecated. Someone else posted something like this:

 Vector3[] points = new Vector3[3];
      //Something something ...
      lineRenderer.positionCount = points.Length;
      lineRenderer.SetPositions(points);

avatar image TheRockRipper wyatts · Jun 24, 2017 at 11:05 AM 0
Share

I was actually looking for a way to dynamically reflect the Raycast so that when if the rotation of the reflective object changes, the Line Renderer adapts to the change. So I looked a lot before I found this post. He explains almost each line and the code works.

Link : http://www.41post.com/4162/program$$anonymous$$g/unity-raycast-reflection

avatar image wyatts TheRockRipper · Jun 24, 2017 at 11:26 PM 0
Share

Looks pretty complicated and he uses stuff that is very out of date. Unity does support reflections through Vector3.reflect

avatar image
0

Answer by Glurth · Feb 02, 2017 at 10:56 PM

Is the ExampleClass position the location of the mirror? Since the originalObject is a variable, I'll assume so. I'll also assume the object has a Collider component on it, that defines the angle of the mirror (at the hit point).

The ray will start at the originalObject, and be directed towards the center of the mirror (uncompiled/tested, example only):

 line.SetPosition (0, originalObject.position);
 Vector3 OriginalToMirrorVector= transform.position-originalObject.position;
 Ray ray = new Ray(originalObject.position, OriginalToMirrorVector);
 //100 is just too arbitrary a length, so lets use the actual distance (vector magnitude) between the object and the mirror
 if(Physics.Raycast (ray, out hit, OriginalToMirrorVector.magnitude))  
 {
    line.SetPosition (1,  hit.point);
    if (hit.collider.tag == "Mirrors") 
    {
          //this assumes, for simplicity, that this object is the only one with the "Mirrors" tag
          reflectedObject.position=hit.point + OriginalToMirrorVector.magnitude * Vector3.Reflect (OriginalToMirrorVector, hit.normal); 
          line.SetPosition (2, reflectedObject.position);
    }
 }
 else //not hit!  sanity check- if this fails, you may not have the collider component enabled.
 {
    line.SetPosition (1, transform.position);
 }
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 wyatts · Feb 06, 2017 at 09:16 PM 0
Share

Thanks for your input! I tried out your method and couldn't get it working. Not sure where it breaks down.

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

7 People are following this question.

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

Related Questions

Create a 2D line reflection (laser trace) 1 Answer

Laser Render Reflect Angle incorrect 1 Answer

Only One LineRenderer Renders 0 Answers

Applying custom forces without rigidbody collisions? 0 Answers

Why does this script check for raycast collision? Is it necessary? 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