Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by Galaroth · Apr 11, 2014 at 01:48 PM · 2dreflectionbouncepong

How do i make something reflect in 2D?

I can't make my ball in Pong bounce off of the player when they collide. I have a pretty decent idea of how to do it in 3D, and tried something similar, but it doesn't work. Right now the ball just bounces (almost) straight upwards, after colliding with the player, no matter where on the player it hits.

Here's what i got:

public class Ball : MonoBehaviour { private Vector2 moveDirection; public float MoveSpeed; // Adjusts direction ball flies after hitting paddle public float PadDirectionAdjust;

 // Use this for initialization
 void Start () {

     moveDirection = Vector2.right;
 
 }
 
 // Update is called once per frame
 void Update () {

     transform.position = new Vector2(transform.position.x, transform.position.y);

     // Translates the gameObject according to direction and move speed
     transform.Translate(moveDirection * MoveSpeed * Time.deltaTime, Space.World);

     // Destroys gameObject if it goes out of screen
     if (transform.position.x >= 12)
     {
         Destroy(gameObject);
     }
 
 }
 void OnCollisionEnter2D(Collision2D coll)
 {
     // If ball hits paddle
     if (coll.gameObject.tag == "Player")
                     Debug.Log ("AUCH!");
     {
         if (MoveSpeed<40)
             MoveSpeed++;

         Vector2 hitpoint = coll.contacts[0].point;

         BoxCollider2D padCollider = coll.collider as BoxCollider2D;

         Vector2 padCenter = padCollider.transform.TransformPoint(padCollider.center);

         padCenter.y -= PadDirectionAdjust;

         Vector2 newDirection = hitpoint - padCenter;

         moveDirection = newDirection.normalized;

         return;

     }

     moveDirection = Vector3.Reflect(moveDirection.normalized, coll.contacts[0].normal).normalized;

     moveDirection = new Vector2 (moveDirection.x, moveDirection.y); 
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
2

Answer by Yorgi · Dec 13, 2016 at 10:59 PM

 try this one

private Vector2 inDirection; private float speed = 1f;

 public void Start()
 {
     inDirection = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
 }

 public void FixedUpdate()
 {
     transform.Translate(inDirection * speed, Space.World);
 }

 public void OnCollisionEnter2D(Collision2D collision)
 {         
     var contactPoint = collision.contacts[0].point;
     Vector2 ballLocation = transform.position;
     var inNormal = (ballLocation - contactPoint).normalized;
     inDirection = Vector2.Reflect(inDirection, inNormal);
 }
Comment
Add comment · Show 2 · 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 Ricsi68 · Nov 28, 2019 at 02:14 PM 1
Share

O$$anonymous$$G man i have been search for this code more than 3 days THAN$$anonymous$$ YOU!

avatar image avictorsa · Apr 24, 2020 at 05:09 AM 0
Share

wow, thats exactly what I was looking for. Thanks

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.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Unity 2D-¿How do I make a bullet bounce off the wall? 4 Answers

Making a pong-like game, and want the ball to reflect according to where on the paddle it hits 1 Answer

Why does my reflect code not work? 1 Answer

pong question 2 Answers

Bounce value not change in script for 2D object in Unity4.3. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges