Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by SaumyaSaurav · Jun 27, 2018 at 02:32 PM · rigidbodyphysics2dbouncecirclepong

Problem with Ball Bouncing after Collision

I am Making a Circle Pong Clone, in that the ball doesn't have a Good Bouncing behaviour i want ball behaviour like in this Video https://www.youtube.com/watch?v=nFnvaThOS30 here is the Code of Ball

Here is what i have Achieved https://drive.google.com/open?id=1kJeD3OfBn6XWoFB15Lern39lZ2oX9EWZ

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class Ball : MonoBehaviour
 {
     public Rigidbody2D rb;
     public Transform centerPoint;
     public float force;
     public Vector2 direction;
 
     Vector3 startPosition;
     float angle;
 
     void Start()
     {
         startPosition = transform.position;
         rb = GetComponent<Rigidbody2D>();
         Invoke("Move", 2f);
         InvokeRepeating("GameOver", 1.0f, 0.8f);
     }
 
     void Move()
     {
         direction = new Vector2(0.5f, -1f);
         rb.AddForce(direction * force);
     }
     void OnCollisionEnter2D(Collision2D col)
     {
         Vector2 norm = col.contacts[0].normal;
         Bounce(norm);
 
     }
     void Bounce(Vector2 normal)
     {
         normal = normal.normalized;
         Vector2 newDirection = Vector2.Reflect(direction, normal);
         newDirection = newDirection.normalized;
         rb.AddForce(newDirection * force);
         direction = newDirection;
 
        // angle = Vector2.Angle()
 
     }
 
     //void CalculateAngel()
     //{
 
     //}
     void GameOver()
     {
         if (Vector3.Distance(transform.position, centerPoint.position) > 1.2f)
         {
             rb.Sleep();
             transform.position = startPosition;
             CancelInvoke("GameOver");
             Invoke("Restart",0.1f);
         }
     }
 
     void Restart()
     {
         Invoke("Move", 2f);
         InvokeRepeating("GameOver", 2.0f, 0.8f);
     }
 }

This Script is added to Ball along with Rigidbody & Circle Collider with a PhysicsMaterial2D Added Anyhelp

Comment
Add comment · Show 1
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 Glurth · Jun 27, 2018 at 02:37 PM 0
Share

If you are using rigid bodies, they should automatically bounce off each other, but I see you have code in Bounce() to compute the bounce also. I'd suggest using only one or the other.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DerperDoing · Jun 27, 2018 at 02:48 PM

@NALAYAK A small gif/video would be helpful in understanding how the ball is bouncing in your case. Never the less, make the ball kinematic then it would respond just to the code.

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 SaumyaSaurav · Jun 27, 2018 at 03:43 PM 0
Share

@DerperDoing Here is The Video $$anonymous$$an, as you can see when the Ball Collides with the Platform it don't bounces back
https://drive.google.com/open?id=1kJeD3OfBn6XWoFB15Lern39lZ2oX9EWZ

avatar image DerperDoing SaumyaSaurav · Jul 01, 2018 at 03:05 PM 0
Share

@NALAYA$$anonymous$$ I haven't tested your code and I'm a beginner(don't know much) but why use the reflect method? Can't you just AddForce( -normal * force)? I mean apply force in the opposite direction of the direction you just got? Will it not work that way?

avatar image
0

Answer by nareshbishtasus · Jun 27, 2018 at 03:09 PM

@NALAYAK No need to write scripts... Just create a physics material in project tab by rightclicking then selecting create then select physcis material and set bounce to one... Play with other properties to get desired result then drag this material to the tab in the collider component attached to the gameobject... I hope this should work.

Comment
Add comment · Show 1 · 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 SaumyaSaurav · Jun 27, 2018 at 03:45 PM 0
Share

I've Already Don't that But i could get the $$anonymous$$ovement Behaviour like i want to achieve watch the video https://drive.google.com/open?id=1kJeD3OfBn6XWoFB15Lern39lZ2oX9EWZ

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

122 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

Related Questions

OnTriggerEnter AddForce 1 Answer

How to calculate a RigidBody2D's bounce from a collider? 1 Answer

Making Pong 1 Answer

Disable bouncing effect 0 Answers

Add Gravitational Attraction to RigidBody2D? 3 Answers


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