Search Unity

Collision issues between Rigidbody and .FBX

Discussion in 'Physics' started by theearthwasblue, Aug 30, 2018.

  1. theearthwasblue

    theearthwasblue

    Joined:
    May 22, 2017
    Posts:
    22
    I'm working on a simple isometric dungeon running game, and I'm having issues getting my player entity to collide properly with an imported .FBX file that is coming in from Blender. The player is designed to move smoothly between a grid layout (similar to the retro pokemon games) and collision works perfectly with created-in-engine primitives, but on colliding with my imported scenery, the player will move a few units into the object, then get stuck - unable to move in any direction - rather than bumping into the object and stopping like the primitives. Its like the .FBX is "soft" and allows the object to penetrate a little, then doesn't know what to do from there so it just halts all movement. Anyway, I'm not sure if this is an issue with my code, or with my imported objects. For the record, I've tried importing other objects that I've used successfully in other applications, and they encounter the same issue; the current scenery is nothing but a mass of equally sized cubes, carefully placed evenly around a grid; and I'm generating colliders automatically on importing the .FBX files. Below is my code:

    Code (CSharp):
    1. {
    2.     Rigidbody Rb;
    3.     public bool block;
    4.     public bool ground;
    5.     public LayerMask playerMask = 9;
    6.     float speed = 6f;
    7.     private Vector3 endpos;
    8.     private bool moving = false;
    9.  
    10.     void Start()
    11.     {
    12.         Rb = GetComponent<Rigidbody>();
    13.         endpos = Rb.transform.position;
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         //setup axis
    19.         float H = Input.GetAxisRaw("Horizontal");
    20.         float V = Input.GetAxisRaw("Vertical");
    21.  
    22.         //apply axis and movement
    23.         if (moving && (transform.position == endpos))
    24.             moving = false;
    25.  
    26.         if (!moving && V != 0)
    27.         {
    28.             moving = true;
    29.             endpos = Rb.transform.position + (Vector3.forward * V);
    30.         }
    31.         if (!moving && H != 0)
    32.         {
    33.             moving = true;
    34.             endpos = Rb.transform.position + (Vector3.right * H);
    35.         }
    36.  
    37.         //check ignore movement if blocked
    38.         block = Physics.CheckSphere(endpos, .25f, playerMask.value);
    39.  
    40.         if (block == false)
    41.         {
    42.             ground = Physics.CheckSphere(new Vector3(Rb.transform.position.x, Rb.transform.position.y - 1, Rb.transform.position.z), .25f);
    43.  
    44.             float step = speed * Time.deltaTime;
    45.             Rb.position = Vector3.MoveTowards(Rb.transform.position, endpos, step);
    46.          
    47.         }
    48.         else
    49.         {
    50.             endpos = Rb.transform.position;
    51.         }
    52.      
    53.     }
    54. }
    55.  

    Any ideas why this code works for Unity's generated primitives, but not my models? Any help would be greatly appreciated, thanks :)
     
    Last edited: Aug 30, 2018
  2. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    First, you do not need to convert blender models to FBX before importing them. Simply bring over your .blend file, not .blend1 though.

    Second, check how your colliders are intersecting to see if there is an issue. Check to see if the collision position is in fact the position stored in code.

    Thirdly, I think your issue is probably with how you are moving the Rigidbody. Personally I like to move rigidbodies with addForce.

    https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
    This allows the physics engine to control most interactions.

    I suspect you are basically teleporting through the object by using MoveTowards.
    You can easily just creat a block as a simulated player and move it into a FBX model using addForce and see if it works.

    https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
    https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html