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

Unexpected Collision Right After Moving and Reactivating GameObject

Discussion in 'Physics' started by wanderbox, Jun 5, 2020.

  1. wanderbox

    wanderbox

    Joined:
    Feb 12, 2020
    Posts:
    3
    The Problem

    I'm making a simple VR 3D pong game with a ball and a paddle. Play goes like this:
    1. The user presses a start round button
    2. The ball GameObject gets its position set to the center of the arena, is given a velocity towards the player, and is set to active
    3. The player volleys the ball back and forth with an AI opponent using a paddle that tracks the VR controller
    4. When the ball gets past the player or the opponent (or the player hits the ball twice on his turn), the round ends, and the ball is set to inactive
    5. Repeat from step 1
    The issue I'm having is that sometimes the ball triggers the paddle's OnCollisionEnter method immediately after a round begins, even though the ball and the paddle are very far apart. (The ball is in the center of the arena and the paddle is on the player's side.) This only happens occasionally, maybe once in ten rounds.

    My Theory

    I can think of one possible explanation for what is happening:
    1. A round ends, and the ball happens to be on the player's side, near where he holds his paddle
    2. Since the round is over, the ball gets set to inactive and remains in its place
    3. The player starts the next round
    4. We move the ball away from the player to the center of the arena (using transform.position =)
    5. The ball is set to active
    6. Even though we set the ball to active after we moved its position, the ball becomes active immediately, and the position of the ball's collider is not really changed until the next physics step. So, there is a brief moment where the ball is active and its collider is sitting on top of the player's paddle. This is what triggers the collision.
    I'm skeptical that this is really the problem, because when I log the position of the ball during one of those weird collisions, I get that it has already moved to the center of the arena.

    In case this was the problem, I tried adding a Physics.SyncTransforms() call right after updating the ball's position, but it didn't fix the problem. I also tried setting the ball's rigidbody to kinematic before updating its position (and switching back afterwards), but still had the issue.

    More Info
    • The paddle has a kinematic rigidbody with continuous speculative collision detection
    • The ball has a non-kinematic rigidbody with continuous collision detection
    Any thoughts about what is happening and how to fix it? Thanks in advance.