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
1
Question by deathmoon781 · Aug 02, 2014 at 06:04 PM · lightray

Reflect ray of light using mirrors?

I want to make a puzzle where the player need to reflect a ray of light light using rotatable mirrors(2d, Im using sidescroller) , what should I do? I have knowledge at Programming. I just cant come up with an algorithm using the unity objects.

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
2

Answer by robertbu · Aug 02, 2014 at 06:09 PM

From the starting position, Raycast() outward. If the Raycast() hits anything, then the RaycastHit.point and the RaycastHit.normal can be used with Vector3.Reflect() to figure out the start and direction of the next Raycast().

Note someone was working on something similar (not precisely this game, but having a ray bounce around) in the last 90 days. His post included a bit of source that might help you, so you might want to poke around on UA to see what you find.

Comment
Add comment · Show 8 · 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 deathmoon781 · Aug 04, 2014 at 04:10 PM 0
Share

Do i need to put the "Raycast() outward" in a script in the object? what is the exact syntax for this? sorry for asking.

avatar image robertbu · Aug 04, 2014 at 04:46 PM 0
Share

Since I have neither a picture or your current source, I can only guess. Typically you will have an object that is the source for the light ray. Since this is 2D, it will probably be the right side of that object that is the front. So assu$$anonymous$$g the script is on the object that is shining the light, the first raycast might be:

 var hit: RaycastHit2D = Physics2D.Raycast(transform.position, transform.right);

For the next hit I'd move up the normal a bit for the position, and you can check the collider to see if it hit anything:

 if (hit.collider != null) {
     nextPos = hit.point + hit.normal * 0.01;
     nextDir = Vector3.Reflect(transform.right, hit.normal);
     var nextHit = RaycastHit2D = Physics.Raycast(nextPos, nextDir);
 
 }

If you are going to allow the light to bounce around, you'll have to structure the next hits into a while loop, walking from one hit to the next. Note there is a possibility of a cycle, so you want to put in some logic that ter$$anonymous$$ates the while loop either when a cycle is detected, or when there is so many bounces that there must be a cycle.

Note the code fragments here are just for demonstration of the concepts. If it were me, I'd fold all this logic into a single function that returned a list of hit points with the initial position and direction passed in as parameters.

avatar image robertbu · Aug 05, 2014 at 02:06 AM 0
Share

I wanted to see the bounce work, so I did a quick prototype. Here is the function that does the bouncing:

 function FindBouncePoints(startPos : Vector3, startDir : Vector3) {
     var pos : Vector3 = startPos;
     var dir : Vector3 = startDir;
     var count : int = 1;
     points.Clear();
     
     var p : Vector3 = startPos;
     p.z = -0.01;
     points.Add(p);
     
     do {
         var hit: RaycastHit2D = Physics2D.Raycast(pos, dir);
         if (hit.collider != null) {
             p  = hit.point;
             p.z = -0.01;
             points.Add(p);
             pos = hit.point + hit.normal * 0.01;
             dir = Vector3.Reflect(dir, hit.normal);
             count++;
         }
     } while (count < maxPoints && hit.collider != null);
 }

It expect 'points' to be declared at the top of the file:

  private var points : List.<Vector3> = new List.<Vector3>();

And here is the result:

alt text

bounce.png (125.4 kB)
avatar image deathmoon781 · Aug 05, 2014 at 02:16 PM 0
Share

sir, im sorry to say this, but Im actually working on 3d, but sidescroller. sorry I put 2D there. :(

avatar image deathmoon781 · Aug 05, 2014 at 02:27 PM 0
Share

It seems there an error: Unexpected Token. in the var nextHit = RaycastHit2D = Physics.Raycast(nextPos, nextDir);

  • prefer using C#

Show more comments
avatar image
0

Answer by Mirecsy23 · Apr 09, 2016 at 06:26 AM

Hi, I made asset that is solved your problem You can get in on AssetStore: http://u3d.as/qBx alt text


9fb536c2-a12f-4677-ad77-d23cfb29a262-scaled.jpg (34.0 kB)
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycasting error? 1 Answer

Up for a challenge? 3 Answers

Ray Light Effect 2 Answers

My raycast is not always in the forward direction. 1 Answer

How is this effect done? 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