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
0
Question by chelnok · Nov 19, 2012 at 05:33 AM · raycastbouncelaserdrawline

Vector3.Reflect - wrong direction

Hi, trying to make bouncing laser. I found few examples using Vector3.Reflect, but cant get it work like i thought it would. I think there is something i cant understand or see anymore, or perhaps Reflect is not ment to be used like this:

 function Update () 
 {
     drawLaser(transform.position,3);
 }
 
 function drawLaser(startPoint:Vector3,n:int)  
 {
     var hit : RaycastHit;
     var rayDir:Vector3 = transform.TransformDirection (Vector3.forward);
      
     for(var i = 0; i < n; i++)
     {     
         if (Physics.Raycast (startPoint, rayDir, hit, 1000)) 
         {
             Debug.DrawLine (startPoint, hit.point);
              rayDir = Vector3.Reflect(startPoint, hit.normal) ;
             startPoint = hit.point;
         }
     }
 }

And here is a picture (white line by code, green one from my mind):

alt text

refprob.jpg (5.1 kB)
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

4 Replies

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

Answer by chelnok · Nov 19, 2012 at 08:54 AM

Found my error after reading (again) other answers, and just staring the code.. Here is the correct code:

 function drawLaser(startPoint:Vector3,n:int)  
 {
     var hit : RaycastHit;
     var rayDir:Vector3 = transform.TransformDirection (Vector3.forward);
      
     for(var i = 0; i < n; i++)
     {     
         if (Physics.Raycast (startPoint, rayDir, hit, 1000)) 
         {
             Debug.DrawLine (startPoint, hit.point);
          rayDir = Vector3.Reflect( (hit.point - startPoint).normalized, hit.normal  ) ;
                 startPoint = hit.point;
         }
     }
 }
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
1

Answer by CodeMasterMike · Nov 19, 2012 at 07:19 AM

The Vector3.Reflect doesn't give you a direction, but a position at the opposite of the orginal vector. So the function is more like a "Set position" for a mirror effect rather than making a laser bounce of a wall.

You have to make your own bounce calculation, which shouldn't be too hard.

For instance:

If the laser bounces of something that is not a floor, then switch the x and/or z direction to the opposite. If the laser hits something that is a floor, change the y direction (if you would want to).

Good luck!

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 chelnok · Nov 19, 2012 at 09:03 AM 0
Share

You almost made me believe this cant be done with Reflect :) Thanks for your opinion anyway, made me to reread docs and couple answers. But yes you are right about opposite of the original vector, i had somekind of misunderstanding there.

avatar image CodeMasterMike · Nov 19, 2012 at 10:04 AM 0
Share

Hehe, I never said it couldn't be done with reflect ;-)

But good that it works and a thumbs up that you put in the code for future reference!

avatar image
0

Answer by test84 · Nov 24, 2012 at 12:33 AM

Why did you normalize the result?

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
0

Answer by chelnok · Nov 24, 2012 at 12:35 PM

well that was just a test, but i wanted to use the result later as direction vector, like: transform.rigidbody.velocity = rayDir * speed;

Dunno how to use code in comment (code,pre doesnt work..) , so i put this as an answer.

 Here is my bouncing lazer script:
 
 var speed:int = 333;
 private var prevPoint:Vector3;
 private var rayDir:Vector3;
 
 function Start () 
 {
     prevPoint = transform.position;
     transform.rigidbody.velocity = transform.forward *speed;
 }
 
 function OnCollisionEnter (col : Collision)
 {
     rayDir = Vector3.Reflect( (col.contacts[0].point - prevPoint).normalized, col.contacts[0].normal ) ;
     transform.rigidbody.velocity = rayDir * speed;
     prevPoint = col.contacts[0].point;
 }
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

12 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

Related Questions

LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer

Mouse controlled Laserpointer 1 Answer

Shoot Laser 0 Answers

Why Do Raycasts Ignore NavMesh Agents 4 Answers

Vector3.Forward not working? 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