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 /
This question was closed May 28, 2019 at 09:16 PM by tonymthediggity for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by tonymthediggity · May 26, 2019 at 04:22 AM · scripting problemraycastvector3

Multiple Raycasts & Vector3.Reflects in a row. How?

I'm trying to make a raycast reflect off multiple surfaces. I can successfully get one reflection but am currently stuck on how to do multiple.

I guess I'm stuck on where to start the second raycast from. Can anyone help me figure this out?

           RaycastHit hit2;
           RaycastHit hit3;
           Debug.DrawRay(transform.position, transform.up * 100f, Color.green);

           //First raycast and reflect
           if(Physics.Raycast(transform.position, transform.up, out hit2, 100f))
             {
                 
                 
                 Vector3 newDir = Vector3.Reflect(transform.up, hit2.normal);
                 Debug.DrawRay(hit2.point, newDir * 100f, Color.green);

                 //Attempting next raycast and reflect.
                 if(Physics.Raycast(hit2.point, newDir, out hit3, 100f ))
             {
                 Vector3 newDir1 = Vector3.Reflect(transform.up, hit3.normal);
                 Debug.DrawRay(hit3.point, newDir1 * 100f, Color.green);
                 
             }



      }













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

  • Sort: 
avatar image
1
Best Answer

Answer by highpockets · May 26, 2019 at 03:04 PM

The second raycast direction is the Vector3 that the Reflect() function returns. Just keep doing the exact same all the way along. You give Reflect an inNormal and inDirection and it gives you the direction you need. Shoot a ray in that direction. The direction returned by the function is the equal magnitude/length of the inDirection, so if you would prefer normalizing it for readablility/usability, just use Vector3.Reflect(inDirection, inNormal).normalized;

Comment
Add comment · Show 2 · 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 tonymthediggity · May 27, 2019 at 01:39 AM 0
Share

I guess I'm just not understanding properly. I can get another ray to fire, but not from where I want.

Right now I'm trying to draw a ray after the second reflection.

 if (Physics.Raycast(newDir, transform.up, out hit2, 100f ))//third ray
                 {
                     Vector3 newDir1 = Vector3.Reflect(transform.up, hit2.normal);
                     Debug.DrawRay(hit2.point, newDir * 100f, Color.red);
                     
                 }

But this is causing a ray to fire way up at the top of the screen. What should I be setting for the new raycast? Currently I'm using newDir, which is the reflected vector3, but I assume that's not correct.

avatar image highpockets tonymthediggity · May 27, 2019 at 06:16 AM 0
Share

Your passing mixed messages to the Raycast function. The first parameter that you are passing should be the origin (start position) of the Ray. The next parameter is the direction. You are passing the newDir first and then transform.up (transform.up is just the normalized y axis direction of the transform that this script is on), but I don’t think you need that even in the first raycast. Say you wanted to shoot your raycast out firstly from your player gun along his forward z axis. $$anonymous$$aybe, you would pass the first parameter gun.transform.position and then you would pass the second parameter gun.transform.forward. Then do your reflection and the you pass the hit.point as the first parameter and then the reflectDirection as the second parameter, but you can just make this raycast/reflect logic a single method

Follow this Question

Answers Answers and Comments

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

Problem With Raycasthit Angle 0 Answers

Spawn script on mesh crashes Unity 0 Answers

How to move the main camera in Google Cardboard? 0 Answers

UI Text showing weird string value? 1 Answer

How to access scripts in other game object with raycasts? 2 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