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 chendryx_disney · May 14, 2017 at 10:22 PM · c#physicsraycastupdateprecision

Phyics.Raycast alternatively returns True/False with static input

I have a script attached to my camera in the FPS Prefab, and in the Update() function I have the following code which effectively shoots a ray from the camera into the world:

 bool result = Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward));
 Debug.Log(result);

When the Raycast does not hit any colliders in the scene, for example if I'm looking up at the Skybox, Physics.Raycast continuously returns False, which is expected.

However, if I point the camera at a collider in the scene, the raycast alternates between returning True/False, ie on one frame it hits the collider, on the next it doesn't. This is true even if the camera doesn't move (ie the input to the Physics.Raycast is identical on every frame).

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 Sergio7888 · May 17, 2017 at 06:36 AM 0
Share

Any Physics action need be put in the FixedUpdate function not in the Update because any Physics action is only executed in the FixedUpdate, FixedUpdate is called in a interval greater than Update for this reason it is less called, when using in Update in the FixedUpdate frame your code will return true but in other frames the result can change.

avatar image AlwaysSunny Sergio7888 · May 17, 2017 at 12:15 PM 2
Share

This is mostly true, but I'm not sure it would cause this particular problem. Writing to the physics system (e.g. creating or moving objects) usually needs to occur in FixedUpdate, but for reading (e.g. raycasts) it shouldn't matter. If you're raycasting in Update, you might get the same result you got last Update, but it shouldn't fail outright.

2 Replies

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

Answer by AlwaysSunny · May 14, 2017 at 10:22 PM

Be sure this script has not been added to multiple objects. That could easily cause the output you're reporting.

Comment
Add comment · Show 2 · 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 Bunny83 · May 17, 2017 at 03:02 PM 0
Share

This is most likely the reason so i converted the comment into an answer.

avatar image chendryx_disney · May 17, 2017 at 04:21 PM 0
Share

Sorry AlwaySunny, I would have replied earlier but I wasn't able to log in for a couple days - this was indeed the problem, I had errantly added the script as a Component to another GameObject.

avatar image
-1

Answer by chendryx_disney · May 18, 2017 at 11:04 AM

@AlwaysSunny - yup that's the ticket, I had accidentally added the script to another GameObject. Thanks!

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3.Movetoward. Why does this work? 0 Answers

Multiple Cars not working 1 Answer

AI Shooting for Aircraft 1 Answer

using A* pathfinding to go to player after raycast has seen it - c# 0 Answers

Change the direction of ScreenPointToRay() Ray 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