Advertisement

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

Started by September 29, 2018 04:12 PM
1 comment, last by ryt 6 years, 4 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.

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