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 /
avatar image
0
Question by ionside · Apr 11, 2016 at 12:12 PM · c#physicsvector3bulletsreflect

Reflect bullets in 3D world space

I want bullet to bounce/reflect mirroring the position they hit. I have it working... kind of. All collision faces facing a particular direction bounce the bullets off close enough to look correct. But any other facing direction of the colliders bounces incorrectly.

 if (reflect)
             {
                 RaycastHit hit;
 
                 if (Physics.Raycast(this.transform.position, Vector3.forward, out hit, 10f))
                 {
                     Vector3 incomingV = hit.normal - transform.position;
                     Vector3 reflectV = Vector3.Reflect(incomingV, hit.normal);
                     //Debug.DrawRay(hit.point, reflectV, Color.green, 10f);
 
                     Transform hitBounce = PoolManager.Pools["AMMO"].Spawn("BoltReflected");
                     hitBounce.position = hit.point;
                     hitBounce.eulerAngles = reflectV;
                 }
 
                 
             }

I have tried a few variations but it just doesn't seem to ever work cleanly. Could someone please point out to me what I've gotten wrong?

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

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

Answer by HarshadK · Apr 11, 2016 at 12:39 PM

It is working for a specific direction only because you are using Vector3.forward in your raycast hit, which will always be the same direction i.e. (0, 0, 1). What you want is the forward direction of your bullet, for which you need to use Transform.forward.

Just change your line of RaycastHit to use forward of your bullet's transform as below:

  if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, 10f))


Comment
Add comment · Show 4 · 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 troien · Apr 11, 2016 at 12:45 PM 1
Share

You beat me to the answer :p

But to expand on the answer:

Vector3.Reflect wants a direction and a normal. Currently only the normal is provided, the other is a bit odd.

The inco$$anonymous$$g direction should be something like the forward of your bullet. (Same as the direction of the raycast).

I'd expect this should work better (assu$$anonymous$$g that this script is added to the bullet, and the bullet is moving in the same direction as his own forward)

 RaycastHit hit;
 
 if (Physics.Raycast(transform.position, transform.forward, out hit, 10f))
 {
     Vector3 reflectV = Vector3.Reflect(transform.forward, hit.normal);
     //Debug.DrawRay(hit.point, reflectV, Color.green, 10f);
 }

Also, the returned value of Vector3.Reflect are not eulerangles. They represent a forward. So setting your eulerangles to a forward should cause some odd behaviour. I'd advise to either set the forward or set the rotation to Quaternion.SetLookRotation.

avatar image ionside troien · Apr 11, 2016 at 01:30 PM 0
Share

Thanks! Looks like I've used a lot of things incorrectly here. I haven't used SetLookRotation before. Would this be the correct way to use it: hitBounce.rotation.SetLookRotation(reflectV, Vector3.up); The bullets are bouncing offset from the hit.point currently.

avatar image ionside troien · Apr 11, 2016 at 02:29 PM 0
Share

Alrighty, sorted! Thanks again @Harshad$$anonymous$$ and @troien. Turns out I was best to delete all I had and rewrite it.

 if (reflect)
             {
                 RaycastHit hit;
 
                 if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, 10f))
                 {
                     Vector3 reflectV = Vector3.Reflect(transform.forward, hit.normal);
 
 
                     transform.position = hit.point;
                     transform.rotation = Quaternion.LookRotation(reflectV);
 
                 }
             }

The spawning of new instances for the bounce was causing some major delay and so the reflect looked like it was offset quite a bit. So removed the spawn and prevented the bullet from despawning on hit.

Thanks again! Was stuck on this for hours.

avatar image ionside · Apr 12, 2016 at 09:09 AM 0
Share

I just needed to make a $$anonymous$$or tweak to prevent the bullets from passing through the collider before bounce.

   if (Physics.Raycast(this.transform.position - this.transform.forward, this.transform.forward, out hit, 10f))

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

141 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

Related Questions

Ricochet Trajectories C# 1 Answer

Multiple Cars not working 1 Answer

Set Velocity at relative position. 2 Answers

What is the error? 2 Answers

Punching objects (C#) 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