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 /
  • Help Room /
avatar image
0
Question by weertoopx · Aug 16, 2017 at 12:55 AM · raycastaddforceraycasthitnormalsnormal

raycasthit.normal sometimes returns zero

Hello, so here is my story, my "player" shoots a blast, the "blast" object has a trigger and when it is shot and hits an object with tag "enemy", it adds a force to the object relative to the normal of the hit.

 void OnTriggerEnter(Collider col)
         {
             if (col.gameObject.tag == "enemy")
             {
                 RaycastHit hit;
                 Physics.Raycast(transform.position, col.gameObject.transform.position, 
                     out hit, DistanceToEnemy(col.gameObject));
                 Debug.Log("Distance To Enemy: " + DistanceToEnemy(col.gameObject));
                 col.gameObject.GetComponent<GetHitEvent>().GetHitEffect(gameObject, hit.normal);
                 Destroy(gameObject);
             }
         }

and here is the GetHitEffect(GameObject, Vector3) function code

 public void GetHitEffect(GameObject blastObj, Vector3 hitAngleVector) //hitAngleVector is the hit.normal
     {
         GetComponent<Rigidbody>().freezeRotation = false;
         GetComponent<Rigidbody>().AddForce(-hitAngleVector * 6, ForceMode.Impulse);
     }

so my problem is that when i fire a blast object and it hits the enemy, sometimes, the hit.normal is zero and i get no force on the object, but other times it calculates the hit perfectly fine.

your help is immensely appreciated.

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

Answer by Johan-Ed · Jun 06, 2018 at 06:59 PM

I think I see a flaw in your code. You use Physics.Raycast and the second parameter of the method you have chosen is direction. The vector between the bullet and the enemy is given by:

 col.transform.position - transform.position

If you want to raycast from your bullet to your enemy, this should be your direction (second parameter).

.

The (potential) error aside, I had the same problem, the Raycast hit had a normal of (0, 0). The only case I know of where this is correct, is if the Raycast in fact hit nothing.

.

Like me, you use OnTriggerEnter to verify a collision before you throw a Raycast, and because of this (and also like me) you dont really have to check whether the raycast hit anything, because you know there is something there.

However if your direction is off, or if the raycast is too short, you might miss the collider you were shooting for. If this happens and you don't verify that you actually hit, then you might end up with a very confusing hit.normal = (0, 0) / (0, 0, 0).

.

In my case it turned out my math was bad and the length of my raycast was (at a specific angle) not long enough, causing no Raycast hit 1/100 times.

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

140 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

Related Questions

A cube, raycasthit and rotation 0 Answers

hit normal vector from raycast is always [-1,0], which makes slope angle 90 1 Answer

RayCast2D and RayDraw errors 0 Answers

raycast : get normal value of linecast hit 1 Answer

Raycast Getting Caught 0 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