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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by grimpunch · Aug 24, 2013 at 02:08 PM · reflectionlaser

Laser Reflection in C#, bouncing the laser from one collider to another with a line renderer.

I am having trouble getting my lasers to react properly, I want them to reflect off the normals of my wedges like a mirror. I adapted a script that created a laser with line renderer points to show it , but I cannot get the direction to change at each collision.

Please help me solve this issue, and understand the solution. thanks.

First here's what it's like while running, then code after. state of the game Code Below:

 using UnityEngine;
 using System.Collections;
 [RequireComponent (typeof(LineRenderer))]
 
 public class LaserEmitter : MonoBehaviour {
     
     public Transform EmmissionLocation;
     public float laserWidth = 1.0f;
     public float noise = 1.0f;
     public float maxLength = 50.0f;
     public Color color = Color.red;
     public LineRenderer lineRenderer;
     public Vector3 direction;
     int length;
     Vector3[] position;
     //Cache any transforms here
     Transform myTransform;
     Transform endEffectTransform;
     //The particle system, in this case sparks which will be created by the Laser
     public ParticleSystem endEffect;
     Vector3 offset;
      // Use this for initialization
     void Start () {
         lineRenderer = GetComponent<LineRenderer>();
         lineRenderer.SetWidth(laserWidth, laserWidth);
         myTransform = EmmissionLocation.transform;
         offset = new Vector3(0,0,0);
         endEffect = GetComponentInChildren<ParticleSystem>();
         if(endEffect)
             endEffectTransform = endEffect.transform;
 
     }
  
     // Update is called once per frame
     void Update () {
         RenderLaser();
     }
  
     void RenderLaser(){
         //Shoot our laserbeam forwards!
         UpdateLength(); 
           lineRenderer.SetColors(color,color);
         
         //Move through the Array
           for(int i = 0; i<length; i++){
             //Set the position here to the current location and project it in the forward direction of the object it is attached to
             offset.x =myTransform.position.x+i*myTransform.forward.x+Random.Range(-noise,noise);
             offset.z =i*myTransform.forward.z+Random.Range(-noise,noise)+myTransform.position.z;
             position[i] = offset;
             position[0] = myTransform.position;
             lineRenderer.SetPosition(i, position[i]);
         }
     }
 
     void UpdateLength(){
 
         //Raycast from the location of the cube forwards
         RaycastHit[] hit;
         direction = myTransform.forward;
         hit = Physics.RaycastAll(myTransform.position, direction, maxLength);
         int i = 0;
         while(i < hit.Length){
             //Check to make sure we aren't hitting triggers but colliders
             if(!hit[i].collider.isTrigger)
             {
                 length = (int)Mathf.Round(hit[i].distance)+2;
                 position = new Vector3[length];
                 direction = Vector3.Reflect(direction,hit[i].normal);
                 //Move our End Effect particle system to the hit point and start playing it
                 if(endEffect){
                 endEffectTransform.position = hit[i].point;
                 if(!endEffect.isPlaying)
                     endEffect.Play();
                 }
                 lineRenderer.SetVertexCount(length);
                 return;
             }
             i++;
         }
         //If we're not hitting anything, don't play the particle effects
         if(endEffect){
         if(endEffect.isPlaying)
             endEffect.Stop();
         }
         length = (int)maxLength;
         position = new Vector3[length];
         lineRenderer.SetVertexCount(length);
     }
 }


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 -hiTo- · Aug 24, 2013 at 04:12 PM 0
Share

I haven't deciphered your code, but I might be able to help you on your way anyway.

You need to do your raycasts in a loop, preferably only when you change the rotation of one of the mirrors. Then raycast to the first mirror (forwards), see if it hits anything (it should, no matter what.)

If it hits something, store the hit.point in a list. Get the new direction of the beam by using Vector3.Reflect() ((hit.point - StartingPoint).normalized, hit.normal). Do this until the Reflect returns back on itself or it reaches it's target.

Now use your stored list of points to create a line renderer :)

avatar image chelnok · Aug 24, 2013 at 05:52 PM 0
Share

check out my q/a for similar topic: http://answers.unity3d.com/questions/350431/vector3reflect-wrong-direction.html

it is unityscript, but if you find it working, you shouldnt have too much problems to make it cs.. its very simple.

avatar image -hiTo- · Aug 24, 2013 at 05:59 PM 0
Share

I actually borrowed that Reflect-stuff from your answer without even knowing it, chelnok :D

Was doing the same thing a while back and fixed it by doing what you did.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Maurice_B · Dec 24, 2014 at 11:48 AM

Still not working? Try this

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
avatar image
0

Answer by evrolittlegames · Jan 31, 2021 at 08:19 PM

Check this tutorial. Maybe this will help you

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

19 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

Related Questions

Vector3.Reflect - wrong direction 5 Answers

Laser Render Reflect Angle incorrect 1 Answer

Problem with reflection using raycasthit2D 0 Answers

Reflective laser 2D, unity. 1 Answer

LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 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