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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Feedback 2019.3 monobehaviour/physics update order broken?

Discussion in 'Scripting' started by Ahlundra, Jul 2, 2019.

  1. Ahlundra

    Ahlundra

    Joined:
    Apr 13, 2017
    Posts:
    47
    or i'm doing something really wrong or everything changed since 2017~18, i've never ran into a problem like this before

    i'm trying to make a turn based project, what i'm doing is making the player move and then everything moves after the player

    but for some stupid reason it seems like everything is trying to move at the same time ignoring each other collision.

    it's like this, player is in x1y1, and move to x2y1... if something tries to move to x2y1 at the same turn, they just ignore the player is already there and move inside the player making the collision go all haywire in the next frame sending everyone like a bullet to random directions lol

    ///player code
    Code (CSharp):
    1.         if (Configuracao.Jogo_Por_Turno && !Mihr.Jogador_Moveu_esse_Turno)
    2.         {
    3.             Vector3 newPos = Vector3Int.RoundToInt(trans.position);
    4.             bool jogador_moveu = false;
    5.  
    6.             if (Input.GetKeyDown(KeyCode.W)) { newPos.y += 1f; jogador_moveu = true; }
    7.             if (Input.GetKeyDown(KeyCode.S) && !jogador_moveu) { newPos.y -= 1f; jogador_moveu = true; }
    8.             if (Input.GetKeyDown(KeyCode.A) && !jogador_moveu) { newPos.x -= 1f; jogador_moveu = true; }
    9.             if (Input.GetKeyDown(KeyCode.D) && !jogador_moveu) { newPos.x += 1f; jogador_moveu = true; }
    10.             if (Input.GetKeyDown(KeyCode.Space) && !jogador_moveu) {jogador_moveu = true; }
    11.  
    12.             if (jogador_moveu)
    13.             {
    14.                 var ray = Physics2D.RaycastAll(Vector2Int.RoundToInt(trans.position), Vector2Int.RoundToInt(newPos - trans.position), 1f);
    15.                
    16.                 if (ray.Length > 1) { jogador_moveu = false; return; }
    17.  
    18.                 trans.position = newPos;
    19.                 Mihr.PlayerPos = newPos;
    20.                 Mihr.Jogador_Moveu_esse_Turno = true;
    21.                 //StartCoroutine(Move_turn());
    22.                 Move_turn();
    23.             }
    24.         }
    25.  
    Code (CSharp):
    1.     public void Move_turn()
    2.     {
    3.         //int b = 0;
    4.         foreach (GameObject o in Mihr.npcList)
    5.         {
    6.             o.GetComponent<AI_Zombie>().Agir();
    7.           //  b++;
    8.  
    9.           //  if (b == 50) { b = 0; yield return null; }
    10.         }
    11.  
    12.         Mihr.Jogador_Moveu_esse_Turno = false;
    13.     }

    and AI code

    Code (CSharp):
    1.     public void Agir()
    2.     {
    3.         //Movendo a criatura
    4.         //==============================================================================
    5.         Vector3 pos = Vector3Int.RoundToInt(trans.position);
    6.  
    7.         if (Vector3.Distance(pos, Mihr.PlayerPos) < 5) //Se o jogador tiver na visão
    8.         {
    9.  
    10.             Vector3 pos2 = Vector3Int.RoundToInt(Vector3.Normalize(Mihr.PlayerPos - pos));
    11.  
    12.             if (pos2.x != 0f) { pos2.y = 0f; }
    13.  
    14.             var ray = Physics2D.RaycastAll(pos, pos2, 1f);
    15.  
    16.             if (ray.Length > 1) { return; }
    17.  
    18.             gameObject.transform.position = pos + pos2;
    19.         }
    20.     }
    21.  
    22. }

    already tried moving using the rigidbody.move function and it's even worse. I know the problem is that for some reason unity isnt updating their bounding box until the end of the frame. If I use a co-routine updating 1 npc per frame for this, everything works as they are supposed to. But I cant use coroutines because it takes too much time and would be pretty stupid to only update one character per frame =/

    is there a way to fix this without making a collision map and checking/updating myself where everything is?
    was there a change on how unity updates collision/physics?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're not using the physics system to move though. You're teleporting by setting transform.position directly instead.

    If this behavior has changed since 2019.1.x, people over in the 2019.3 alpha test forum might be interested in this.
    https://forum.unity.com/forums/2019-3-alpha.337/

    FYI, if you're working on a serious project, you should be using a stable version of Unity rather than an unfinished alpha version.
     
  3. Ahlundra

    Ahlundra

    Joined:
    Apr 13, 2017
    Posts:
    47
    as I said I have tried to use the rigdbodie movement but it's even worse ;/

    dunno if it was a change or it's just me doing something very wrong, the code IS working as expected as long as I move one character PER frame

    that means I need to move one character, wait for the end of the frame for the physics to update every character collision and them move another one

    I remember it updating the position of everything as soon as a change happened, but right now if I move object 1 from point x10y10 to x15y15, if I try to move a second object, it still thinks the first object is in position x10y10 instead of x15y15 for the ray/physics functions

    no matter how I move it be it through rigidbodie.moveto or directly to the transform

    the only thing I didnt use is the apply/add force... because it's supposed to move in fixed distances (1f) like chess and not pixel by pixel

    I feel like i'm going crazy lol
    the code IS right but it's not working as it supposed to if I move everything in the same frame =s

    ====
    yeah you're right, i'm using this version because i'm messing with ECS
    probably it's some bug or just something that changed

    but probably i'm doing something wrong, maybe there is a call to update the physics that I forgot or something like that, or maybe my code is wrong by itself