🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Breakout's like game physics problem: ball's movement.

Started by
1 comment, last by ryt 5 years, 9 months ago

Hi, 

I'm developing a breakout's like game and I've a very simple problem with physics.
Here's my code:
 

Code (CSharp):
  1.  public void FixedUpdate()
  2.     {
  3.         if (ballInPlay)
  4.         {
  5.             // Ball movement
  6.             transform.position += direction * speed * Time.fixedDeltaTime;
  7.         }
  8.     }
  9.  
  10. public virtual void OnCollisionEnter2D(Collision2D collision)
  11.     {
  12.         // Change direction after collision
  13.         if (ballInPlay)
  14.         {
  15.             GameObject go = collision.gameObject;          
  16.             direction = Vector2.Reflect(direction, collision.GetContact(0).normal);
  17.         }
  18.     }

Basically the ball's movement is erratic, every time the ball hits something, it change its acceleration but direction is right. I don'use any physic material because I need some custom behavior.

Thank you for replying.

Advertisement

From the function names I would say that you are using Unity3D. I haven't done in a while collisions there but if I remember correctly it could be something with your rigid body or physics material on the ball.

Also OnCollisionEnter2D() is declared as virtual. It could be that something is overriding the behavior.

This topic is closed to new replies.

Advertisement