• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cimel · Jan 05, 2019 at 03:55 PM · unity 5unity 2dcollider2dreflect

Breakout-style paddle control problem

Hello. I am working on a Breakout-style Unity game and I am having an issue the interaction between the ball and the paddle. I want to make it so that depending on where the ball hits the edges of the paddle it will angle out more along the x axis. The way my code is currently working the balle always keeps the same angle no matter where it hits the paddle. Here is my code for the ball bouncing off the paddle. Thanks for any help!

 if (collider.gameObject.name == "Paddle")
 {
           float x = hitFactor(transform.position, collider.transform.position, collider.collider.bounds.size.x);
                     Vector2 dir = new Vector2(x, 1).normalized;
                     GetComponent<Rigidbody2D>().velocity = dir * ballSpeed * Time.deltaTime;
 }
         
 private float hitFactor(Vector2 ballPos, Vector2 paddlePos, float paddleWidth)
 {
         return (ballPos.x - paddlePos.x) / paddleWidth;
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sean244 · Jan 05, 2019 at 08:20 PM

This is how I did it in my own breakout clone and it seemed to do the trick.

[UPDATE] I've replaced the coroutine with an invoke statement, and replaced the 'if statements' inside OnCollisionEnter2D with a cleaner switch statement. The code still runs as expected.

 using UnityEngine;

 public class Ball : MonoBehaviour
 {
 [SerializeField]
 private float _speed;
 
 private Rigidbody2D _rb;
 
 private Vector2 _direction;
 
 private void Start()
 {
     _rb = GetComponent<Rigidbody2D>();
     Invoke("DropBall", 1);
 }
 
 private void DropBall()
 {
     int random = Random.Range(-1, 2);
     _direction = new Vector2(random, -1).normalized;
 }
 
 private void FixedUpdate()
 {
     _rb.velocity = _direction * _speed;
 }
 
 private void OnCollisionEnter2D(Collision2D other)
 {
     switch (other.gameObject.tag)
     {
         case "Paddle":
             {
                 float x = HitFactor(transform.position, other.transform.position, other.collider.bounds.size.x);
                 _direction = new Vector2(x, 1).normalized;
                 break;
             }
         case "SideWall":
             {
                 _direction.x *= -1;
                 break;
             }
         case "Ceiling":
             {
                 _direction.y *= -1;
                 break;
             }
     }
 }
 
 private float HitFactor(Vector2 ballPos, Vector2 paddlePos, float paddleWidth)
 {
     return (ballPos.x - paddlePos.x) / paddleWidth;
 }

 }

demo.gif (500.6 kB)
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cimel · Jan 07, 2019 at 06:12 AM 0
Share

Thanks for the reply. I created a script based on what you used, and now whenever the ball hits the paddle it teleports to position 0,0, and launches from there.

 public class Ball : $$anonymous$$onoBehaviour
 {
 
  [SerializeField]
     private float _speed;
 
     private Rigidbody2D _rb;
     private Vector2 _direction;
 
     void Start()
     {
         _rb = GetComponent<Rigidbody2D>();
         StartCoroutine(DropBall());
     }
 
     IEnumerator DropBall()
     {
         yield return new WaitForSeconds(1f);
 
        float random = Random.Range(-1.0f, 2.0f);
         _direction = new Vector2(random, -1).normalized;
     }
 
     private void FixedUpdate()
     {
        _rb.velocity = _direction * _speed;
     }
 
     private void OnCollisionEnter2D(Collision2D other)
     {
         Vector2 normal = other.contacts[0].normal;
 
         if (other.gameObject.name == "Paddle")
         {
             float x = HitFactor(transform.position, other.transform.position, other.collider.bounds.size.x);
             _direction = new Vector2(x, 1).normalized;
         }
         if (other.gameObject.tag == "SideWall")
         {
             _direction = Vector2.Reflect(_direction, normal);
             _direction.Normalize();
         }
         if (other.gameObject.name == "Ceiling")
         {
             _direction = Vector2.Reflect(_direction, normal);
             _direction.Normalize();
         }
     }
 
     float HitFactor(Vector2 ballPos, Vector2 paddlePos, float paddleWidth)
     {
         return (ballPos.x - paddlePos.x) / paddleWidth;
     }
 }
avatar image sean244 Cimel · Jan 07, 2019 at 07:20 AM 0
Share

Can you edit your post to show the updated code?

avatar image Cimel sean244 · Jan 07, 2019 at 08:19 AM 0
Share

Sure thing. I've edited the post.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Jumping from Obstacle while adding velocity to Player 0 Answers

Platform Collider Not Working When Enemy Collides With It 1 Answer

Trigger Triggers Without Collision 1 Answer

Can anyone help with loading issues when button is clicked? 0 Answers

How do I get the square root of a double number value? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges