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 /
  • Help Room /
avatar image
0
Question by Kidroflz · Apr 09, 2017 at 12:57 AM · c#bouncepong

not the bounce iam looking for

i am currently trying to make a pong game for learning purposes. my current problem is i have found codes to make a ball bounce...however the ball bounces on the puck just fine but when it brushes against the wall it will either shoot directly up or directly down opposed to more of a diagonal bounce. i still believe what iam looking for is something rather simplistic...but i just cant wrap my head around it. also iam a newbie to unity; only been using the software for a week or so sooo.... yeah please help meh public class SphereCollision : MonoBehaviour { public Rigidbody RbSphere; public float mag; public float Constantspeed; #region

     #endregion
 
     // Use this for initialization
     void Start () 
     {
         RbSphere = GetComponent<Rigidbody>();
         mag = RbSphere.position.magnitude;
         Constantspeed = 1.0f;
 
 
     }
     void Udate()
     {
 
     }
 
     void OnCollisionEnter(Collision CollisionInfo)
     {
         
         var direction = RbSphere.GetPointVelocity (transform.localEulerAngles);
         RbSphere.AddForce(Vector3.Reflect(direction, CollisionInfo.contacts[0].normal) * mag, ForceMode.Impulse);
         RbSphere.velocity = Constantspeed * (RbSphere.velocity.normalized);
     }
 
     // Update is called once per frame
 
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by OfficialCoatsee · Apr 09, 2017 at 06:26 AM

Just a suggestion, I'm not sure if this will work for you - but try adding the rubber material to the mesh on your walls, and see if that makes any sort of difference.

Alternatively, what you could to, is detect what exactly the ball is colliding against, before applying the bounce code.

So...

 void OnCollisionEnter(Collision CollisionInfo) {
          if (CollisionInfo.transform.tag == "wall") {
                //use less?
          } else {
                var direction = RbSphere.GetPointVelocity (transform.localEulerAngles);
                RbSphere.AddForce(Vector3.Reflect(direction, CollisionInfo.contacts[0].normal) * mag, ForceMode.Impulse);
                RbSphere.velocity = Constantspeed * (RbSphere.velocity.normalized);
 }


Let me know if any of this either helps you or doesn't.

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 Kidroflz · Apr 20, 2017 at 12:18 AM 0
Share

sorry it took so long to reply but yeah i ended up realizing that i needed to delete gravity and from that point at start i added a continuous force at a random range on the x and y axis... its running pretty smoothly now, thank you sir :]

avatar image
1

Answer by Kidroflz · Apr 20, 2017 at 12:42 AM

i deleted gravity and added a script for random range

             sx = Random.Range (-10, 10) == 0 ? -1 : 1;
         sy = Random.Range (-10, 10) == 0 ? -1 : 1;
 
         Rbsphere.velocity = new Vector3(Random.Range(-15,15) * sx, Random.Range(-15,15) * sy, 0);

then after that i added that code into the re spawn method. i even eventually added the code i used above into the collision script to make the bounce //in my opinion seem more fluid.

 Direction = Rbsphere.GetPointVelocity(Rbsphere.transform.localPosition);
         Rbsphere.AddForce(Vector3.Reflect(Direction ,Ballcol.contacts[0].normal)*Mag, ForceMode.Impulse);

Mag being a variable that i state in the start function Mag = Rbsphere.transform.position.magnitude /13;

i divided it by a number because the results i would get from mag would make the bounce of the ball seem un-natural. hope this helps other people in the future, btw these are a combinations of codes pulled from tutorials and from the unity learn tab, i know someone wont sue me but shout out to those who helped others befor hand.

Comment
Add comment · 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

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

330 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Raycast direction change problem 0 Answers

2D Enemy AI problems. 1 Answer

Struggling with bounce pad in platformer game. 1 Answer

Update instatiate doesn't work? 1 Answer

Why does my Pong paddle code work? 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