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 ShroomWasTaken · Jul 10, 2017 at 05:54 PM · rigidbodyvector3reflect

Weird results when using Vector3.Reflect and RaycastHit.normal?

I'm having a problem where my sphere sometimes just goes through an object, bounces back as if the velocity was inverted, and sometimes also reflects it, but mirrored?

Not sure how I should explain, so here's a -video-.


My code for the reflecting and stuff is here :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bullet : MonoBehaviour
 {
     private Rigidbody m_bulletRigidbody;
     private RaycastHit m_rayHit;
     private bool m_raycastDidHit;
 
     private void Start()
     {
         m_bulletRigidbody = GetComponent<Rigidbody>();
     }
 
     private void Update()
     {
         Debug.DrawRay(transform.position, m_bulletRigidbody.velocity.normalized, Color.red);
         m_raycastDidHit = Physics.Raycast(transform.position, m_bulletRigidbody.velocity.normalized, out m_rayHit);
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (m_raycastDidHit)
         {
             if (other.gameObject.tag != "Player" && other.gameObject.tag != "LocalPlayer" && other.gameObject.tag != "Bullet")
             {
                 if (m_rayHit.transform.gameObject.tag != "Bullet" && m_rayHit.transform.gameObject.tag != "LocalPlayer" && m_rayHit.transform.gameObject.tag != "Player")
                 {
                     Vector3 reflected = Vector3.Reflect(m_bulletRigidbody.velocity.normalized, m_rayHit.normal);
                     reflected.x *= m_bulletRigidbody.velocity.x;
                     reflected.y *= m_bulletRigidbody.velocity.y;
                     reflected.z *= m_bulletRigidbody.velocity.z;
                     m_bulletRigidbody.velocity = reflected; 
                 }
             }
         }
     }
 
 }


Anyone who can explain why this is happening? :o

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
0
Best Answer

Answer by Jinkata · Jul 10, 2017 at 06:03 PM

I'm guessing this is moving fairly quickly considering it's a bullet and if that's the case then the physics loop to check for collisions may be missing some things. One thing you can do is keep track of the last position each frame and shoot a raycast from the last position to the new position to handle collisions that way instead of relying on the physics engine.

If you need to clarification just let me know.

Comment
Add comment · Show 13 · 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 ShroomWasTaken · Jul 10, 2017 at 06:48 PM 0
Share

Alright so as far as I can see, the suggestion which you had seems to be working when it comes to physics. None of them seem to be ignored, and all of them seem to be detecting a "broken ray / collision".

However, some of the bullets still go through, but I'm pretty sure it's a Vector3.Reflect problem since when I walk to the other side of the same object, it suddenly bounces off.

And I still seem to have the problem where the bullets bounce off surfaces unrealistically.

Here's a video of what happens now.

And here's my attempt at implementing what you suggested :

 private void Update()
     {
         m_raycastDidHit = Physics.Raycast(transform.position, m_bulletRigidbody.velocity.normalized, out m_rayHit);
         if (m_raycastDidHit && m_rayHit.distance <= Vector3.Distance(transform.position, m_lastPos))
         {
             Debug.DrawLine(transform.position, m_rayHit.transform.position, Color.red);
             if (m_rayHit.transform.gameObject.tag != "Player" && m_rayHit.transform.gameObject.tag != "LocalPlayer" && m_rayHit.transform.gameObject.tag != "Bullet")
             {
                 if (m_rayHit.transform.gameObject.tag != "Bullet" && m_rayHit.transform.gameObject.tag != "LocalPlayer" && m_rayHit.transform.gameObject.tag != "Player")
                 {
                     Vector3 reflected = Vector3.Reflect(m_bulletRigidbody.velocity.normalized, m_rayHit.normal);
                     reflected.x *= m_bulletRigidbody.velocity.x;
                     reflected.y *= m_bulletRigidbody.velocity.y;
                     reflected.z *= m_bulletRigidbody.velocity.z;
                     m_bulletRigidbody.velocity = reflected;
 
                 }
             }
         }
     }


avatar image Jinkata · Jul 10, 2017 at 07:18 PM 0
Share

I need to test tonight, but I think I may have an idea what's going on. Not sure though. I'll see if I can't reproduce your results, or if this is a project you're willing to share so I can download it I would be more than willing to try it from there too.

avatar image ShroomWasTaken Jinkata · Jul 10, 2017 at 09:23 PM 0
Share

I made a little line drawing function that doesn't clear the lines right away like Debug.DrawLine does, and I can now say 100% that the problem where my bullets go through the walls are not physics related, since the physics does detect a collision and the reflection code is called. So I'm pretty sure it's an issue with my reflection code.

avatar image Jinkata · Jul 10, 2017 at 10:16 PM 0
Share

Ins$$anonymous$$d of this:

 Vector3 reflected = Vector3.Reflect(m_bulletRigidbody.velocity.normalized, m_rayHit.normal);
 reflected.x *= m_bulletRigidbody.velocity.x;
 reflected.y *= m_bulletRigidbody.velocity.y;
 reflected.z *= m_bulletRigidbody.velocity.z;
 m_bulletRigidbody.velocity = reflected;

Try this:

 Vector3 reflected = Vector3.Reflect(m_bulletRigidbody.velocity.normalized, m_rayHit.normal) * Vector3.magnitude(m_bulletRigidbody.velocity);
 m_bulletRigidbody.velocity = reflected;
avatar image Jinkata · Jul 10, 2017 at 10:18 PM 1
Share

$$anonymous$$ight need to wrap that Vector3.magnitude in a $$anonymous$$athf.Abs to make sure it's not a negative number.

avatar image ShroomWasTaken Jinkata · Jul 11, 2017 at 09:30 AM 0
Share

Alright, so I tried that and the results are much better, however still not perfect. I noticed that sometimes some bullets would still go straight through a wall, no matter how big the wall was, so I did some more testing, and I noticed that it only seemed to happen when I fired many bullets at once. When I tried using a auto rifle ins$$anonymous$$d of a shotgun the bullets worked just as expected. Could it somehow be that the bullets from the shotgun "spray" are "obstructing" each other's raycasts? I can't figure why they would if they do, but I can't think of any other reason to why it would do this. And also, Vector3.magnitute should have an uppercase $$anonymous$$, Vector3.$$anonymous$$agnitude :]

And also, where should I upload the project files, if you still need them?

avatar image ShroomWasTaken ShroomWasTaken · Jul 11, 2017 at 11:39 AM 0
Share

And I also noticed that the bullets always go through an object on their 3rd or 4th bounce.

This is so confusing o.O

Show more comments

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

92 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

Related Questions

Can you pls help me fix this rigidbody collision problem 0 Answers

Change player movement 0 Answers

Enemy AI help with height check 1 Answer

Cardboard VR: Drag object towards player but have it stop just before 0 Answers

NavAgent Speed & Rigidbody Velocity 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