• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

142 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 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

Why am I getting a Null Reference Exception? 0 Answers

Display GameObjects tag using Raycast 1 Answer

Invisible normals 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges