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 Marshal_Alessi · Feb 14, 2016 at 05:47 AM · c#raycastupdatefixedupdate

Why does FixedUpdate work when Update() doesn't?

Hello. Quick preface, I read through the API on Update and FixedUpdate, but I didn't fully understand why this changed my code... Basically, I wrote a code for a dialogue script which checks the distance via raycast and if its touching the Face collider, and initiates the chat if the player is close enough and all parameters are met. Let me be clear, this script works perfectly using FixedUpdate, but when using Update(), it doesn't every time, almost as if there is lag and it only works some of the time...Can someone explain why?

     if(Physics.Raycast(raycast.transform.position,(forward),out hit)){
             print ("raycast hit");
             Distance = hit.distance;
             print (Distance + " " + hit.collider.gameObject.name);
             if(Distance < 3 && hit.collider.gameObject.name == "Face" && Input.GetKeyDown ("e") && chat_on != true){
             print ("E was pressed and chat is not true");
             NPCS = GameObject.FindGameObjectsWithTag ("NPC");
             foreach (GameObject obj in NPCS) {
                 objPos = obj.transform.position;
                 mag = (objPos - player.transform.position).sqrMagnitude;
                 if(nearestNPC == null || (nearestNPC.transform.position - player.transform.position).sqrMagnitude > mag){
                     print ("the mag calcs");
                     nearestNPC = obj;
                 }
             }
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

2 Replies

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

Answer by TreyH · Feb 14, 2016 at 06:28 AM

Physics and Collision operations occur during FixedUpdate(). Since you are calling Physics.Raycast (which checks for colliders along a given Ray), you will often want to use them in FixedUpdate(). Collider detection should still be fine in other places (like Update), but any raycasting that result in a manipulation of physics objects should be done during FixedUpdate() calls or yield instructions.

edit: clarifying.

http://docs.unity3d.com/Manual/ExecutionOrder.html

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 NoseKills · Feb 14, 2016 at 10:47 AM 1
Share

I can't offer a better explanation but i wonder if this is entirely true since even the Unity manual shows an example where raycasting is done in Update

One thing to remember though is that Input gathering happens in sync with Update(), so with this code in FixedUpdate, Input.Get$$anonymous$$eyDown ("e") can happen multiple times in a row with one key press (if framerate is slower that physics update interval)

avatar image Marshal_Alessi NoseKills · Feb 14, 2016 at 06:37 PM 0
Share

Thank you for your answer Trey. @Nose$$anonymous$$ills, I saw that page in the API as well, which is what led me to this confusion. I mean, I know you can put a Raycast in Update but clearly its not guaranteed to work every time. With my script, even if fixedupdated ran something more than once, it wouldn't cause any harm due to the (chat_on) boolean which prevents the script from re-starting. Still, it is cause to wonder why the API has a raycast in plain old Update().

avatar image
0

Answer by eco_bach · Feb 20, 2021 at 03:04 AM

I believe this is incorrect. Physics.Raycast should only be used in FixedUpdate if you are also altering the forces on some rigidibody as a result. Could someone confirm?

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 TreyH · Feb 20, 2021 at 02:47 PM 0
Share

Yeah, the raycast will just check for any collider in its path, regardless of when you call it. I think OP's situation might've had other stuff going on that happened to sync up with FixedUpdate. I use raycasts in Update for static objects and haven't had any issues.

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

91 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

Related Questions

Raycast2D in update? 1 Answer

Need help with my code 0 Answers

Update() and FixedUpdate() not running on C# scripts 0 Answers

FixedUpdate and Update not working in script 0 Answers

how to check if an object is betwen enemy and player 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