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
1
Question by nicodrew · Feb 08, 2018 at 02:37 PM · collisionraycastinginteraction

Ray cast passes through an object when it should record a hit.

I have code which is meant to allow the player to interact with objects by pressing the E key. This has worked for most objects in my game so far but when trying to implement ammo pick ups in the form of small 9mm rounds it stopped working.

Using Debug.Log statements I can see that the ray cast seems to be able to hit everything in the scene apart from the round. The round is on the same Default layer as all my other interactable objects.

    void FixedUpdate() {
         //leave interaction check
         interactionEnding = false;
         if (interacting)
         {
             if (Input.anyKeyDown)
             {
                 leaveInteraction();
             }
         }
         float distance = Vector3.Distance(player.position, transform.position);
         //interaction check
         if (distance <= radius)
         {
             //if E pressed and not currently leaving an interaction
             if (Input.GetKeyDown(KeyCode.E) && !interactionEnding)
             {
                 Ray ray = new Ray(player.position, player.forward);
                 RaycastHit hit;
                 if (Physics.Raycast(ray, out hit, 100))
                 {
                     Debug.Log("ray hit " + hit.transform.name);
                     if (hit.collider.gameObject == gameObject)
                     {
                         interact();
                     }
                 }
             }
         }
     }

Initially I thought the round was too small to be hit so in order to debug I scaled it up 10x. Still when I face the round and try to interact the debug console returns "ray hit wall(34)" essentially meaning that the ray passes through the round and hits the wall behind it.

The round has the Interactable script component which contains the above code along with a box collider which surrounds the mesh.

I hope these images help:

The inspector. As you can see the layer is set to default. Ignore the odd size dimensions as these don't seem to affect the issue alt text

The round itself with the box collider wrapped around perfectly. The round is depth shaded and as the unity editor doesn't display depth shading it appears matte grey however most objects in my game are also depth shaded so this shouldn't affect anything either alt text

9mm-round-inspector.png (148.9 kB)
9mm-round-depth-shaded.png (14.3 kB)
Comment
Add comment · Show 1
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 nicodrew · Feb 08, 2018 at 02:42 PM 0
Share

As an additional comment. When running the code in Update ins$$anonymous$$d of FixedUpdate the effect is the same

1 Reply

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

Answer by nicodrew · Feb 08, 2018 at 04:39 PM

I hate to answer my own question but I solved the problem. As you can see in the inspector I passed the FPScontroller(Transform) to the variable player. This worked fine in the past when the FpsController and FirstPersonCharacter shared a transform however I recently changed the Y axis co-ordinate of the FirstPersonCharacter as I wanted the player to appear taller.

Using debug.drawRay I noticed the ray was originating from my players body and not the player camera meaning for large objects it was accurate enough but for small object it would miss.

By resetting the transform of the FirstPersonCharacter I fixed this issue. To make the character taller I've decided to play with the scale settings of fps controller.

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 nicodrew · Feb 08, 2018 at 04:43 PM 0
Share

Better Still, I've decided to change the player height using the height variable in the character controller. I hope this helps anyone who stumbles across the same problem. In short, adjusting character height by moving the First person camera is bad practice.

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

172 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Raycast not always detecting floor 1 Answer

Player teleports in a random direction 0 Answers

Raycast does not seem to properly detect object it hit 0 Answers

My collisions won't work whenever I pick it up with the XR Toolkit 0 Answers

Interaction with Objects in VR, massive Objects 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