Advertisement
Ryoh

HitWallExample

Sep 10th, 2022 (edited)
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1.     public void HitWall(Collision2D collision)
  2.     {
  3.         // We don't want the reflect to decide the x direction.
  4.         // Store the current direction.
  5.         var direction = desiredVelocity.x;
  6.        
  7.         // Get the surface we contacted.
  8.         ContactPoint2D contact = collision.GetContact(0);
  9.  
  10.         // DesiredVelocity is a variable defined at the top of the class. It is used to keep track of the desiredDirection, and speed.
  11.         // Figure out the new y trajectory.
  12.         desiredVelocity = Vector2.Reflect(desiredVelocity, contact.normal);
  13.  
  14.         // Set x back to the stored direction.
  15.         desiredVelocity.x = direction;
  16.  
  17.         // Use the new desiredVelocity to adjust the rigidbody velocity.
  18.         AdjustVelocity();
  19.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement