Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bumping kinematic objects??

Discussion in 'Scripting' started by cgraf1, Dec 4, 2018.

  1. cgraf1

    cgraf1

    Joined:
    Jan 11, 2015
    Posts:
    35
    I have what seems like a small issue, but I really am having trouble getting it to work and I am not sure why.

    I have a few objects on a platform that are all supposed to be bumping into each other. Each one of these objects is a parent with a kinematic rigid body which has 3 children, all with colliders and kinematic rigidbodies of their own. I have a script on one of the children, the main "Body" of these objects, that has a script to be pushed back in the opposite direction of the object that they hit. This is happening with OnTriggerEnter.

    For some reason sometimes the object go right through each other, instead of the opposite direction, sometimes they go in some random direction. It works great when only one is moving and bumping into a stationary one, but if they are both going full speed, then that is when things go awry. It is even worse when there are more than 2 on the platform.

    Here is the script below:

    Code (CSharp):
    1.  
    2. public class BodyCollision : MonoBehaviour {
    3.  
    4.      private bool hit = false;
    5.      private bool moving = false;
    6.      public float distance;
    7.      float time = 1.0f;
    8.      Vector3 dir;
    9.      GameObject parent;
    10.  
    11.      void OnTriggerEnter(Collider col)
    12.      {
    13.          if(col.gameObject.tag == "Island")
    14.          {
    15.              Debug.Log("Hello");
    16.          }
    17.          else if(col.gameObject.tag != "Island")
    18.          {
    19.              if(hit == false)
    20.              {
    21.                  hit = true;
    22.                  if(col.gameObject.tag == "Body")
    23.                  {
    24.                      Debug.Log("happened");
    25.                      dir = transform.root.position - col.transform.root.position;
    26.                      dir.y = 0f;
    27.                      parent = col.transform.root.gameObject;
    28.                      if(parent.tag == "Kart")
    29.                      {
    30.                          dir = -dir;
    31.                          parent.GetComponent<BotController>().bumping = true;
    32.                      }
    33.                      else if(parent.tag == "Player")
    34.                      {
    35.                          parent.GetComponent<PlayerController>().bumping = true;
    36.                      }
    37.                      moving = true;
    38.                  }
    39.                  hit = false;
    40.              }
    41.          }
    42.      }
    43.  
    44.      void Update()
    45.      {
    46.          if(moving == true)
    47.          {
    48.              transform.root.Translate(dir * (Time.deltaTime * (distance/time)));
    49.              StartCoroutine(BumpDelay());
    50.          }
    51.        
    52.      }
    53.  
    54.      IEnumerator BumpDelay()
    55.      {
    56.          yield return new WaitForSeconds(0.1f);
    57.          moving = false;  
    58.      }
    59. }
    60.  
    bumping stops the Player and other Bots from moving during the bumps, which is why that is set to 'true' on the bump, but after they are called back to false in their respective control scripts. If it makes a difference, the player is moved with transform.Translate and the Bots are moved with MoveTowards().

    I am also using kinematic objects because I want real control of the bumps. I had non-kinematic rigidbodies before with AddForce() on bumps and they would get all tangled up.

    I would appreciate any help I can get. Thank you so much.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. cgraf1

    cgraf1

    Joined:
    Jan 11, 2015
    Posts:
    35
    Right. I am using kinematic rigidbodies and OnTriggerEnter because I want more control over the bumps with code as opposed to relying on physics. So in the script I am applying a transform.Translate in the opposite direction of the collider for each Trigger. (I forgot to Normalize the direction, but have added that since with no better results), but they are not always moving in the correct direction. So that has been my form of adding artificial physics. Is this not proper?

    I have also since tried changing all of the OnTriggerEnter's into OnCollisionEnters and setting up the Physics Manager to enabled kinematic-kinematic collisions, but these were not better results either. Any ideas?
     
    Last edited: Dec 4, 2018
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    It's difficult to troubleshoot this sort of this remotely, without even seeing what is happening, so you probably have to handle this yourself.
    Add lots of debug.log's and debug.drawline's so you can draw/see things like collision direction to make sure it's what you expect, maybe use debug.Break to pause the sim at the moment of collision. Isolate the circumstaces that create the unwanted reaction and then troubleshoot those specific scenarios ('are they moving too fast' [overpenetration], 'is the angle too shallow' [ignore near misses?], 'should these units actually be colliding' [team assignment], etc)
    Your posted code is not super complex, so it's probaly not at fault (excpet maybe line 30)