Search Unity

Organisation and Accuracy of Physics Tasks

Discussion in 'Physics' started by Matt-Face, Feb 10, 2015.

  1. Matt-Face

    Matt-Face

    Joined:
    Mar 24, 2013
    Posts:
    22
    Hi All,

    I am somewhere between beginner and intermediate with C# and unity, i was hoping to get some help with trying to determine the best way to organise my code. It always tends to get very messy when it comes to Physics/Physics2D tasks. Typically when not dealing with Physics tasks i would put everything into a method or coroutine so that the individual processes are separated out into their functions within each class and everything is organised/easier to follow.

    However, I have read that i need to put my Physics tasks inside the FixedUpdate loop as this is triggered less frequently than the Update loop. Lately i am wondering if i might have misinterpreted this information as this is consistently making an absolute mess of my code. I generally want to use collisions in what ever i am doing so i use Physics/Physics2D to move all objects which require collisions & movement etc...

    Is it possible to still do Physics/Physics2D tasks inside methods or coroutines? If so, are there any requirements like can these only be triggered from inside the FixedUpdate Loop? would it matter if it is applying a single force to an object or a continuous or changing velocity etc?

    A current example i am struggling with is a case where i want to move a Rigidbody2D along a series of Vector3 points in sequence. how can i correctly implement this to avoid overshoot and change to the next target correctly if FixedUpdate is not being called every frame? This would be trivial to accomplish using the objects transform and coroutines. Unfortunately i am struggling to find the correct approach to these sorts of tasks using Physics/Physics2D.

    happy do do my own research/reading on any topics if someone can just point me in the right direction.

    Thanks in advance!
     
  2. Matt-Face

    Matt-Face

    Joined:
    Mar 24, 2013
    Posts:
    22
    Okay so i have just found what i have been missing:

    Code (CSharp):
    1.  yield return new WaitForFixedUpdate();
    seems like correctly written coroutines and Methods will be fine after all...
     
  3. Roni92

    Roni92

    Joined:
    Nov 29, 2013
    Posts:
    225
    It's not true that you HAVE to make physics tasks in fixed update... Its just recommended.