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
0
Question by wakeupscreaming · Jul 15, 2012 at 12:32 AM · javascriptrandomballbounce

Ball Bounce with Random Range

Following a tutorial, I have created a ball that bounces along 3 walls with a paddle at the bottom, like the game Pong. Sometimes the ball gets stuck in a straight line bounce loop, bouncing from one wall to the other and back with no variation. I'd like to add a bit of randomness to the bounce angle to the ball so that it doesn't get stuck.

I found a similar question answered already, but it was answered with C#, and the tutorial script is in javascript. Everything I tried did not work. Would Random range and vector help? I don't know how to implement them into the script.

Part of the script added to the ball:

 function Awake(){
  rigidbody.AddForce(8, 4, 0, ForceMode.Impulse);
  InvokeRepeating("IncreaseBallVelocity", 2, 2);
 }
 
 function IncreaseBallVelocity(){
  rigidbody.velocity *= 1.05;
 }
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 wakeupscreaming · Jul 17, 2012 at 04:19 AM 0
Share

Can anyone just translate the C# Answers from "Breakout Pong Ball Stuck to Wall Problem" to Javascript? Please help me. http://forum.unity3d.com/threads/138770-Breakout-Pong-ball-stuck-to-wall-problem

It's just a direction of vector problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Jul 15, 2012 at 12:41 AM

I guess you could just add some force when it hits:

  function OnCollisionEnter(other : Collision) {
       rigidbody.AddForce(Random.insideUnitCircle * 10, ForceMode.Impulse); // or some other multiplier
  }
Comment
Add comment · Show 9 · 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 wakeupscreaming · Jul 17, 2012 at 02:00 AM 0
Share

I tried this and I thought it might have worked. But today it got into another loop. The ball started bouncing from one side of the wall, to the other, and back and forth. I was hoping a little random variation in it's bounce angle would get it out of those loops?

$$anonymous$$aybe the title of my question is wrong. I'm really looking for some random bounce in the ball, specifically in the angles, when it hits a surface. So if the ball bounces onto a surface at 90 degrees, maybe the bounce off would be 88, or 91 degrees (just a random degree or 2 off) the other way.

avatar image whydoidoit · Jul 17, 2012 at 02:57 AM 0
Share

Try a bigger force?

avatar image wakeupscreaming · Jul 17, 2012 at 03:12 AM 0
Share

I used a higher force. What I'm noticing is that the ball then has a higher propulsion -- it goes faster, or slower. I'm not really looking for something that goes faster, or has anything to do with the speed of the ball at all.

I want the angle of the bounce to be random. Here is a similar question, not the exact problem, but similar: http://forum.unity3d.com/threads/138770-Breakout-Pong-ball-stuck-to-wall-problem

All the answers are in C#. I'm working in Javascript.

Thanks.

avatar image whydoidoit · Jul 17, 2012 at 03:15 AM 0
Share

Right so take the velocity magnitude before you apply the force and map it back after:

    var speed = rigidbody.velocity.magnitude;
    rigidbody.AddForce(Random.insideUnitCircle * 1000, Force$$anonymous$$ode.Impulse); // or some other multiplier
    rigidbody.velocity = rigidbody.velocity.normalized * speed;
    
avatar image wakeupscreaming · Jul 17, 2012 at 03:55 AM 0
Share

Just FYI, with a 1000, it still got into a vicious loop bounce cycle from wall to wall. What does the 1000 mean? So if 1000 still gets the ball bouncing from one wall to the other, then should I put in 10000?

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Ping Pong Reflect 2 Answers

How do i make a Character/object bounce using Javascript? 2 Answers

Script not working (Java script) 2 Answers

Code to randomly generate a mesh? 0 Answers

Random spawn timer 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