Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Fixed TimeStep and performance

Discussion in 'Physics' started by kader1081, Aug 2, 2023.

  1. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    Hi, i have a very fast object and collision and physic werent working properly so after researching best way to make this was reducing fixed timestep. I reduced x10 what would be performance difference
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Don't adjust the fixed time-step like this. Use continuous detection or physics queries. If you do 10x the number of physics calls it could slow things down in a number of ways. It's only 10x worse if you do exactly the same thing each simulation step i.e. same bodies, same contacts etc. It could be 5x worse or 15x worse. If it swamps the total cycle time of the player-loop then it'll spiral out of control with fixed-update never being able to do 10x calls per loop so it runs even more then that gets worse so it has to run more.

    So, it'll be much worse (performance wise) is the answer but the only thing you should be asking this question to is the profiler as it's the only thing that has access to your project and your device.

    Also, consider, "FixedUpdate" is not "Physics". It's a step in the player-loop that physics is passively hooked into but so are lots of other systems including your scripts. Increasing this frequency, increases all of those too. It's like thinking "Update" is for renderers only, it's not.

    Changing the fixed-update is the wrong tool.
     
    kader1081 likes this.
  4. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    I did i made rigidbody continuous bit still missed. Maybe i should try raycast. I looked profiler it is twice worse fps. Is there any way for reducing timestep just for collision detection
     
    Last edited: Aug 2, 2023
  5. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    I lowered 0.02 to 0.01 it rarely misses and fps dropped about 20 fps.Is there any way for reducing fixed timestep just for collision detection. can raycast calculate more accurate.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Not really but you are free to simulate physics manually decoupled from the fixed time-step but I don't see how that will help unless you quantify what the "20 fps" is from. Running physics at a specific rate won't be better if you call it rather than Unity. Also, if running physics at a higher frequency is helping, that just sounds like the problem is you're using discrete.

    As the docs state though, continuous is limited to specific collider types and you don't provide any detail, just super broad questions. All that said, I'm not a 3D physics dev here at Unity either.

    I will add that this all sounds like an XY problem. :) It's best to try to figure out why you're gettting tunneling. Often that's best done in a new simple test project to test assumptions etc. Maybe it's some physics (PhysX) limitation but maybe not.

    EDIT: In 2023.1, simulation stage options were added but this feels like a distraction from the problem but thought I'd mention it: https://docs.unity3d.com/2023.1/Documentation/ScriptReference/SimulationStage.html
     
    kader1081 likes this.
  7. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    There is a bullet bullet has capsule collider it set to collider not trigger it has rigidbody with continuous collision detection i am creating and setting velocity with this code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class turretFireAnim : MonoBehaviour
    6. {
    7.  
    8.     public GameObject TurretShell;
    9.     public GameObject ProjectileLoc;
    10.     public GameObject fireExplosion;
    11.     float time = 2f;
    12.     public TurretRotatiton rotate;
    13.     public bool isPlaced;
    14.     AudioSource source;
    15.     public AudioClip fireSound;
    16.     void Start()
    17.     {
    18.         source = GetComponent<AudioSource>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.    
    25.     }
    26.  
    27.     public void setCanTurn()
    28.     {
    29.  
    30.  
    31.         rotate.canTurn = true;
    32.     }
    33.     public void Fire()
    34.     {
    35.         rotate.canTurn = false;
    36.         source.PlayOneShot(fireSound,0.1f);
    37.         Quaternion targetRotation = Quaternion.LookRotation(rotate.direction.normalized, Vector3.up);
    38.  
    39.         GameObject Bullet = Instantiate(TurretShell, ProjectileLoc.transform.position, targetRotation);
    40.         Instantiate(fireExplosion, ProjectileLoc.transform.position, targetRotation);
    41.  
    42.         Bullet.GetComponent<Rigidbody>().velocity = rotate.direction.normalized * 2000f;
    43.         Bullet.GetComponent<Turret_1_Projectile>().enemy = rotate.Target;
    44.  
    45.  
    46.  
    47.  
    48.     }
    49. }
    50.  
    and there is a enemy it has rigidbody with continuous detection and it has box collider which set to trigger.When bullet collide with enemy i am destroying it ontriggerenter here is the code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Turret_1_Projectile : MonoBehaviour
    6. {
    7.     public GameObject enemy;
    8.     public GameObject explosion;
    9.  
    10.     public Color craterColor = Color.black;
    11.     float maxCollapseDepth = 3f;
    12.     float time;
    13.     float GeneralTime;
    14.     float craterSize = 5f;
    15.     Rigidbody rig;
    16.     float timer = 5f;
    17.     Vector3 speed;
    18.  
    19.     void Start()
    20.     {
    21.         rig = GetComponent<Rigidbody>();
    22.         speed = rig.velocity;
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         timer -= Time.deltaTime;
    29.         if(timer<=0f) Destroy(gameObject);
    30.       // Guide();
    31.     }
    32.  
    33.  
    34.     private void OnTriggerEnter(Collider collision)
    35.     {
    36.  
    37.         if (collision.gameObject.tag=="Enemies")
    38.         {
    39.        
    40.             Destroy(collision.transform.parent.gameObject);
    41.             Destroy(gameObject);
    42.             Vector3 explosionPosition = collision.transform.position;
    43.             Instantiate(explosion, explosionPosition, Quaternion.identity);
    44.  
    45.             Destroy(collision.transform.parent.gameObject);
    46.  
    47.        
    48.      
    49.  
    50.      
    51.    
    52.  
    53.  
    54.         }
    55.      
    56.  
    57.  
    58.  
    59.  
    60.     }
    61.     private void OnCollisionEnter(Collision collision)
    62.     {
    63.  
    64.  
    65.         if (collision.gameObject.tag == "hey")
    66.         {
    67.  
    68.             Vector3 incidentDirection = rig.velocity.normalized;
    69.             Vector3 reflectionDirection = Vector3.Reflect(incidentDirection, collision.contacts[0].normal);
    70.        
    71.  
    72.             transform.rotation = Quaternion.LookRotation(rig.velocity, Vector3.up);
    73.             rig.velocity = speed.magnitude * reflectionDirection;
    74.             int layer = LayerMask.NameToLayer("FriendBulletsRico");
    75.             gameObject.layer = layer;
    76.             // collideTerrain(collision);
    77.  
    78.  
    79.             return;
    80.         }
    81.  
    82.     }
    83.  
    84.    
    85.  
    86.  
    87.  
    88.     }
    89. }
    90.  
    because of fast movement sometimes bullet goes through enemy
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    I'm not sure if triggers are detected using continuous or not in 3D (PhysX). They certainly are NOT in 2D physics because continuous involves detecting the point of impact (POI) and there is no POI with triggers.
     
    kader1081 likes this.
  9. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Yes, triggers aren't solid and so they can obviously move through other objects. Disable the trigger and use OnCollisionEnter to detect the collision and destroy enemy.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    kader1081 and zulo3d like this.
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    You're much better off using physics queries to do this though. You can cast the shape (sphere, ray etc) forward by the projectile velocity * Time.fixedDeltaTime and get your answer without the need for a collider on the projectile.
     
    kader1081 likes this.
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    PhysX dev came back and confirmed that continuous does not apply to triggers.

    This is why you're getting discrete col-det behaviour.
     
    Edy and kader1081 like this.
  13. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    thank you maybe i should refactor my code for collision instead of trigger .I dont think this is an option because. I want the bullet to pierce the enemy, but when 2 colliders collides , it loses its speed. Maybe I can add speed after the collision
     
    Last edited: Aug 2, 2023
  14. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,423
    Good to know! I think this totally worth a mention in the manual when describing the continuous collision options.
     
    MelvMay likes this.
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    You can use a physics query (as I mentioned above) for this and personally I think it's far easier anyway than relying on the simulation to provide you callbacks with the added bonus it doesn't actually interact with the scene. Raycast, sphere/capsule cast etc.
     
    kader1081 likes this.