Search Unity

Resolved Collision is not working properly

Discussion in 'Scripting' started by paksisoma, Feb 23, 2024.

  1. paksisoma

    paksisoma

    Joined:
    Aug 15, 2018
    Posts:
    5
    I have two objects. The blue object has a Box Collider, and the red object has a Mesh Collider with Rigidbody.

    The blue object works as follows:

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (direction == -1 && transform.position.z < -7.3)
    4.     {
    5.         direction = 1;
    6.     } else if (direction == 1 && transform.position.z > -4.7)
    7.     {
    8.         direction = -1;
    9.     }
    10.  
    11.     transform.position += new Vector3(0, 0, direction) * Time.deltaTime;
    12. }
    There is a video about the problem. After the red object falls, the blue object collides through the red object instead of pushing.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,420
    The Blue object has a Static Collider i.e. non-moving as it doesn't have a Rigidbody.

    You should be moving it Kinematically so add a Rigidbody, make it Kinematic and use MovePosition on it. Also, only do that in FixedUpdate because physics doesn't run per-frame by default, it runs during the FixedUpdate cycle.
     
    paksisoma likes this.
  3. paksisoma

    paksisoma

    Joined:
    Aug 15, 2018
    Posts:
    5
    Problem solved, thank you very much.
     
    MelvMay likes this.