0
\$\begingroup\$

I am currently in the process of creating my first game (as a hobbyist) and I have an issue with Unitys Rigidbody.

The objective of the game is to steer a ball in a 2-d-ish environment (3-d rigidbody that is constrained to two dimensions) by moving (e.g. rotating) the platforms the ball touches. Basically this works kind of well, but movent after hitting a platform "feels" a bit off. When the platform rotates, the ball kind of slides off instead of bouncing, supposedly because the movement of the platform is not taken into account when the ball hits it.

Animated GIF showing the current behavior.

I set the types of the mesh colliders of the platforms to convex and non-trigger. The rigidbody calculation is "continuous dynamic". The platform colliders are pill-shaped (basically cylinders with demi-sphere on both ends).

Is there any built in way to take the platforms dynamics into account with the rigidbody or will I have to roll my own physics implementations? (It feels that this would be quite difficult, since I'd have to calculate the speed of the platform at the point of the collision and somehow take this into account!?)

\$\endgroup\$
5
  • \$\begingroup\$ Hm, for some reason the GIF does not show the actual issue, but is cut, I'll try to fix it. \$\endgroup\$
    – Paul K
    Commented Nov 2, 2020 at 11:55
  • \$\begingroup\$ How are you moving your platforms at present? \$\endgroup\$
    – DMGregory
    Commented Nov 2, 2020 at 12:40
  • 1
    \$\begingroup\$ @DMGregory Your comment actually helped me finding my issue. It struck me tonight while lying awake :) In the provided sample I used simple colliders which I moved by changing their rotation. After switching to rigidbodies for the platforms, too, and adding a torque, it plays way better now. \$\endgroup\$
    – Paul K
    Commented Nov 4, 2020 at 8:55
  • 1
    \$\begingroup\$ That sounds like it would be worth posting as an answer. :) For context: when you move an object with its Transform component, that's treated as a teleportation by the physics engine. It instantaneously switches to a new position/angle, rather than moving there, so there's no linear or angular velocity behind the motion for it to impart on objects it hits along the way. Moving with physics using the Rigidbody puts that oomph behind the motion. :) \$\endgroup\$
    – DMGregory
    Commented Nov 4, 2020 at 10:40
  • \$\begingroup\$ @DMGregory Done. :) \$\endgroup\$
    – Paul K
    Commented Nov 4, 2020 at 11:01

2 Answers 2

2
\$\begingroup\$

Fortunately, the comment

How are you moving your platforms at present?

gave me the hint what I was doing wrong: I used the transform component of my platforms for rotation, which was abviously wrong, because

when you move an object with its Transform component, that's treated as a teleportation by the physics engine. It instantaneously switches to a new position/angle, rather than moving there, so there's no linear or angular velocity behind the motion for it to impart on objects it hits along the way[.]

The correct way of doing this, would not be using the transform component of the platform to rotate it, but adding a rigidbody component. This way I was able to add a torque to the rigidbody, which allows Unity to get the physics right and calculate bounces correctly.

In order to prevent the platform drifting away and exposing other unwanted behavior, I constrained the translation of the rigidbody in all 3 dimensions and the rotation in the x- and y-dimension. Furthermore I disabled gravity (which should be unnecessary with the constrained translation, but to be sure ...) and set the rigidbody to "continuous dynamic".

\$\endgroup\$
1
  • \$\begingroup\$ Indeed. When you change the transform directly, you are not moving the object, you are teleporting the object. As far as the physics engine is concerned, the object disappears from reality and reappears with a new rotation. \$\endgroup\$
    – Philipp
    Commented Nov 4, 2020 at 11:51
0
\$\begingroup\$

What appears to be happening (correct me if I am wrong) is the loss of energy during a collision. This would be a possible reason why the ball was not being given adequate momentum after the bounce.

I encountered the same issue while creating bouncy bullets for a 2D Metroidvania for Ludum Dare 47.

One quick fix I found was to add a Physics Material to the bouncing object and set its Bounciness attribute to 1 while keeping the Friction at 0. Your game looks like it depends on the torque provided by the rotating platforms to project the ball. In that case, you could try playing around with the bounciness to adjust the difficulty and skill required to master the mechanics. Good luck with your project.

\$\endgroup\$
4
  • \$\begingroup\$ Thank you very much, I'll give it a try. \$\endgroup\$
    – Paul K
    Commented Nov 2, 2020 at 12:43
  • \$\begingroup\$ Unfortunately it did not make a difference (well it did, but there was no real improvement whatsoever) \$\endgroup\$
    – Paul K
    Commented Nov 3, 2020 at 7:42
  • \$\begingroup\$ @PaulK Can you explain your problem in more detail? \$\endgroup\$
    – WRECKER_1
    Commented Nov 3, 2020 at 7:51
  • 1
    \$\begingroup\$ Yeah, I'll give it a try, but later, I'm on my day job now :) \$\endgroup\$
    – Paul K
    Commented Nov 3, 2020 at 8:44

You must log in to answer this question.

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