1
\$\begingroup\$

I found this question, but I'm not sure if it applies to my problem.

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.) Sufficiently fast mouse movement eventually moves the paddle 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:

public class MouseMovement : MonoBehaviour {
    public float speed = 10f;
    // Update is called once per frame
    void Update () {
        var translation = Input.GetAxis("Mouse Y") * speed;
        translation *= Time.deltaTime;
        transform.Translate(0, 0, translation);
    }
}

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

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Its simple, always remember when you are moving objects manually or through Physics.

Here you are moving object manually (through Translate) and expecting Physics to perform accurately, this is the problem. Your manual command will overwrite the Physics functionality.

You can try to restrict your object through your code that its Z value should not be greater and less than your limits.

OR

Second move your object through Physics instead of Translate like change in velocity or apply force

Its been a long time, correct me if I am wrong. I will welcome everyone.

\$\endgroup\$
0
\$\begingroup\$

Unity's CharacterController can handle this, it's a bit clunky to use at first (I thought) but it's how I addressed this exact same problem.

https://docs.unity3d.com/ScriptReference/CharacterController.html

Instead of transform.translate, you'll use the CharacterController's Move() or SimpleMove(). I found SimpleMove() to be much more useful.

https://docs.unity3d.com/ScriptReference/CharacterController.SimpleMove.html

Happy hunting.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .