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
-1
Question by Impstar · Feb 28, 2017 at 09:29 PM · raycastraycasthit

Raycast check not returning false

I am attempting to create a hovering player by using a raycast that exerts an upwards force if said player is less than a certain distance away from the ground. The problem is, the raycast is always detecting the ground being within that certain distance, even if the player is much too far away. The result is that my player just keeps floating up.

 private RaycastHit hitInfo;

 void RayCheck () { //fires Raycast down
     Physics.Raycast(transform.position, Vector3.down, out hitInfo, 2f);
     Debug.DrawRay (transform.position, Vector3.down * 2f);
     if (hitInfo.distance < 2) {
         rb.AddForce (Vector3.up * hoverForce);
     }
 }

The script thinks that hitinfo.distance is always less than 2, even when it clearly isn't. Does anyone have an idea of why this is happening? Or if the idea just fundamentally won't work?

Comment
Add comment · Show 2
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 Hanoble · Feb 28, 2017 at 10:18 PM 0
Share

Have you tried putting the Physics.Raycast in an if and only running the logic if it actually hits something? Such as:

 void FixedUpdate()
 {
       RaycastHit hitInfo;
 
       if (Physics.Raycast(transform.position, Vector3.down, out hitInfo, 2.0f))
       {
             if(hitInfo.distance < 2)
             {
                    rb.AddForce(Vector3.up * hoverForce);
             }
       }
 }
avatar image Impstar Hanoble · Feb 28, 2017 at 10:35 PM 0
Share

Thank you! It appeared that this was the problem.

2 Replies

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

Answer by CaffeineAndCoffee · Feb 28, 2017 at 10:18 PM

In this case, the raycast should be inside of an if loop,

RaycastHit hitInfo; 
void RayCheck () {
    if (Physics.Raycast (transform.position, Vector3.down, out hitInfo, 2f)) {
            rb.AddForce (Vector3.up * hoverForce);
        } 
    }

You may have to play around with the Rigidbody settings and the hoverforce float to get the desired effect.

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 Impstar · Feb 28, 2017 at 10:30 PM 0
Share

Thanks! I will need to tinker with it so I'm not bobbling up and down, but this does appear to be what the doctor ordered!

avatar image
0

Answer by jwulf · Feb 28, 2017 at 10:16 PM

I could imagine, the Raycast hits the player-object itself?

As a fourth parameter for Raycast, you can assign a LayerMask (https://docs.unity3d.com/ScriptReference/Physics.Raycast.html) of layers to ignore. Set your player's layer (which is, I assume, different from the Ground Layer) there and if this was indeed the problem, it should'nt happen again.

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

88 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

Related Questions

Raycast won't hit Collider 1 Answer

Not getting right value; ScreenPointToRay 0 Answers

help the build button in the build settings is grayed out 0 Answers

Raycast not registering hits when mouse is moving fast. 2 Answers

How to make my raycast also hit deactive objects WITHOUT showing the object? 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