Search Unity

Box collider/rigid body eventually moves through static collider

Discussion in 'Getting Started' started by davidleefox, Dec 11, 2017.

  1. davidleefox

    davidleefox

    Joined:
    Nov 11, 2013
    Posts:
    2
    I'm hacking together a Pong-style game, and my problem is with the walls (box collider, no rigid body) not completely stopping movement of the player paddle (box collider, yes rigid body.) As the mouse moves faster in the up direction, eventually the paddle moves through the wall. There is initial resistance, but I would expect `Player Paddle` to stop moving abruptly at the wall. How can I make this happen? I'm new to Unity/colliders/game dev.

    --
    My player paddle simply moves up and down (Z-axis) with up/down mouse movement:


    Code (CSharp):
    1.  
    2.     public class MouseMovement : MonoBehaviour {
    3.         public float speed = 10f;
    4.         // Update is called once per frame
    5.         void Update () {
    6.             var translation = Input.GetAxis("Mouse Y") * speed;
    7.             translation *= Time.deltaTime;
    8.             transform.Translate(0, 0, translation);
    9.         }
    10.     }
    --
    I exported an asset package that reproduces the problem. My problem objects are `Player Paddle`, `Upper`, and `Lower` https://www.dropbox.com/s/yx9svb51nywdvgg/pong-export.unitypackage?dl=0
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Since you're directly manipulating the Transform of the paddle, you're overriding the physics system's attempts to stop it from moving through the collider. You're on the right track, though, as I think you'd probably be better off doing it this way. You'll just need to check to ensure you're not exceeding the bounds of where the paddle can be and skipping the Transform update if so (or rolling the position back to the last allowed position).

    There's a lot of different ways you can do this, and I'm a bit busy with work at the moment or I'd write you out a little sample script. I have to believe there are a ton of tutorials available for Pong clones for the entrepreneurial Googler, though. :p