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

Physics Different in Playmode vs Stepthrough

Discussion in 'Scripting' started by ErnestoBananez, Dec 14, 2015.

  1. ErnestoBananez

    ErnestoBananez

    Joined:
    Sep 23, 2014
    Posts:
    16
    Hi everyone, I'm trying to implement 2D physics and collision detection in Unity using math instead of Unity's physics system so I can bypass the myriad problems Unity seems to have with 2D platformers (default capsule colliders sliding off edges, not being able to handle slopes consistently). I'm following this guy's code and well, frankly it hasn't worked too well so far. I know the ideas behind it are solid though, so I'm trying to fix things up myself so I understand it better. I'm very new to this type of code-it-yourself physics.

    Anyway, one of the problems I'm having is that I'm noticing my scene behaves in completely different ways depending on whether I just press play in the scene or whether I press pause before pressing play and then step through the scene frame by frame. When I step through the scene the box I'm testing the physics on falls and then stops when the raycast detects it's about to collide with an object (I have it in code to transform.translate() to become flush with the object it collides with and that isn't working, but that's a question I'm asking in another post). Either way, it stops BEFORE actually colliding with the object, which is at least part of what I intended. I have a picture of the exact moment when this happens, here:
    Stepthrough.png
    (you can see the first, blue ray I'm using to raycast downwards and check for the ground has intersected with the ground in this frame, causing the others to its right not to fire)

    The above example happens consistently when I step through the scene frame by frame, but when I just press the play mode button, it looks like the square falls through the blocks it's colliding with for just a moment, before fixing itself and becoming flush with the ground. I've been able to pause it at the exact second it dips below the ground, here:
    Playmode.png

    Does anyone have any idea why this is? Unity just seems to glitch out when you run it in standard play mode... I've attached the full script I'm using to do all of this (PhysicsTest.cs), so if anyone wants to actually see what's causing this you can. Below you'll find the inspector of the object I'm using this script on.
    Inspector.png

    I'd be eternally grateful if anyone could give me some info on this. I played with it for a few hours but I'm just stumped.

    Thanks!
     

    Attached Files:

    Last edited: Dec 14, 2015