• 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 dot · Dec 10, 2010 at 02:32 AM · raycastmath

bullet reflected

think about bullets "reflected". meaning: imagine a bullet fired (with raycast) in 2d game and hit a metal wall - it will bounce adhering to "the angle of incidence equals the angle of reflection" rule

alt text

how to calculate it, knowing it's a 2d game, and it will use RaycastHit. am I looking at a solution somewhere here: http://unity3d.com/support/documentation/ScriptReference/RaycastHit.html ?

edit:

function Update () {
var hit : RaycastHit;
if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit))
Debug.DrawRay(hit.point, hit.normal * 10, Color.red);
var incidenceAngle = hit.point - Camera.main.transform.position;
Debug.DrawRay(hit.point, -incidenceAngle* 10, Color.yellow);
var reflectionAngle = hit.normal + incidenceAngle;
Debug.DrawRay(hit.point, reflectionAngle * 10,Color.green);
}

Here I'm drawing normal, incidence angle and reflection angle, but I'm not really doing great with reflection angle, do I..

edit2: in http://www.cs.jcu.edu.au/Subjects/cp2060/2001/lectures/illumination/node9.html r = 2n (n.l)-l;

what's that "(N.L)" and why it's inserted after "2n" without any "+" or "*" or anything that could suggest what to do with it?

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 Mike 3 · Dec 10, 2010 at 06:16 AM

Should just be this:

var reflectionAngle = Vector3.Reflect(incidenceAngle, hit.normal);
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 _Petroz · Dec 10, 2010 at 07:39 AM 0
Share

$$anonymous$$ike you are too good.

avatar image
0

Answer by Zib Redlektab · Dec 10, 2010 at 04:39 AM

If your bullets are only ever hitting vertical walls, just reverse the x-direction of the bullet's movement (or reverse the x-speed, does the same thing) to get it to reflect like above.

I think RayCastHit is more for 3D spaces than 2D, but I could be wrong about that.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How would I find the closest Vector3 point to a given Ray? 2 Answers

How to get the distance between two objects in feet/meter? 1 Answer

2D Raycast in any direction 1 Answer

Finding Distance between Angles and Points 2 Answers

Raycast visual and moving text possition. 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