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;
}
}
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.
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)
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().
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?
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
Follow this Question
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