0
\$\begingroup\$

I have been learning Unity for last couple of days. I want to make one simple ping pong game. The idea is simple. The game has a Sphere as a ball, a plane as ground, 2 rectangles as bats and 4 rectangles as walls. Look at the image first image

It is a 2 players game. One player will use 'W' and 'S' key and another player will use 'Up Arrow' and 'Down Arrow' on the keyboard to move the bat up and down.

But I have a problem here. If a player moves the bat at the limit (up or down), the bat does not detect the collision with the wall. It just goes through the wall. Look at the image. enter image description here

The bats and the walls have Box Collider, but still the problem persists. So what should I do so that the bat detects the collision with the wall.

\$\endgroup\$
1
  • \$\begingroup\$ make sure you have attached rigidbody componenet \$\endgroup\$
    – idurvesh
    Commented May 29, 2015 at 18:51

2 Answers 2

1
\$\begingroup\$

Use Raycast to collider: http://docs.unity3d.com/ScriptReference/Collider.Raycast.html

Just like in example:

  void Update() {
    if (Input.GetMouseButtonDown(0)) {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (coll.Raycast(ray, out hit, 100.0F))
            transform.position = ray.GetPoint(100.0F);

    }
  }

Replace input method and use ray pointed from bat to the wall to detect collision with walls. But in your case I DEFENITELY check bat coordinate to not exceed wall bounds (Y or Z coordinate), because it is simple and I suggest do this for you if want to study coding

Good luck!

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

Oh this... I tried to make a pinball game. There were several methods I tried which included making the hitbox larger, decreasing the time slice for the physics engine (the time in between each physics check was smaller) and a few other things. In the end I had to hand code the physics of the ball to shoot off when it touched the paddles, as long as the paddles weren't at rest. https://www.youtube.com/watch?v=hg3AyHEEGws I will add a link to the project soon. (a reminder wouldnt hurt)

https://www.dropbox.com/s/hraflyt9tj67ikh/ATLATLfinalWithSQL.zip?dl=0

\$\endgroup\$

You must log in to answer this question.

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