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 51mickey · Apr 03, 2012 at 01:36 AM · javascriptcollisiondetection

My Javascript collision isn't being detected, please help.

Javascript question. I need to make a simple game for class. I've made a game with a bouncing basketball within a box, and a moving paddle along the bottom. when the ball collides with the paddle, i want it to continue bouncing. If the user doesn't get the paddle there in time, the game is over. For some reason, the collision isn't being recognized, the ball goes right through the paddle. What am I doing wrong? Here is the code:

#playingArea{ position: absolute; top: 100; left: 100; border: 3px solid black; width: 500; height: 500; background-color: #E0FFFF; } #paddle{ position: absolute; top: 470; left: 228; width: 64; height: 16;
     }
     #ball{
         position: absolute;
         top: 4;
         left: 200;
         width: 16;
         height: 16;
     }
     #score{
         position: absolute;
         top: 486;
         left: 0;
         width: 500;
         height: 14;
         font-size: 10pt;
         color: white;
         background-color: rgb(32,128,64);
     }
 </style>
 <script language="JavaScript">
 //get info, process data, update screen objects
     //instance vars
     var ball;
     var paddle;
     var score;
     //initial speeds
     var dx = 5;
     var dy = 5;
     var currentScore = 0;
     var timer;
     //set initial conditions for ball and paddle
     var paddleLeft = 228;
     var ballLeft = 300;
     var ballTop = 4;
     
     function init(){
         //instantiate HTML object instance vars
         ball = document.getElementById('ball');
         paddle = document.getElementById('paddle');
         score = document.getElementById('score');
         //register key listener with document object
         document.onkeydown = keyListener;
         //start the game loop
         start();
     }
     
     function keyListener(e){
         if(!e){
             //for IE
             e = window.event;
         }
         if(e.keyCode==37 && paddleLeft > 0){
             //keyCode 37 is left arrow
             paddleLeft -= 4;
             paddle.style.left = paddleLeft + 'px';
         }
         if(e.keyCode==39 && paddleLeft < 436){
             //keyCode 39 is right arrow
             paddleLeft += 4;
             paddle.style.left = paddleLeft + 'px';
         }
        
     }
     
     function start(){
         //game loop
         detectCollisions();
         render();
        
         
         //end conditions
         if(ballTop < 470){
             //still in play - keep the loop going
             timer = setTimeout('start()',50);
         }
         else{
             gameOver();
         }
     }
     
     function detectCollisions(){
         //just reflect the ball on a collision
         //a more robust engine could change trajectory of ball based
         //on where the ball hits the paddle
         if(collisionX())
             dx = dx * -1;
         if(collisionY())
             dy = dy * -1;
     }
     
     function collisionX(){
         //check left and right boundaries
         if(ballLeft < 4 || ballLeft > 462)
             return true;
         return false;
     }
     
     function collisionY(){
         //check if at top of playing area
         if(ballTop < 4)
             return true;
         //check to see if ball collided with paddle
         if(ballTop > 470){
             if(ballLeft > paddleLeft && ballLeft < paddleLeft + 64)
                 return true;
         }
         return false;
     }
     
     function render(){
         moveBall();
         updateScore();
     }
     
     function moveBall(){
         ballLeft += dx;
         ballTop += dy;
         ball.style.left = ballLeft;
         ball.style.top = ballTop;
     }
     
     function updateScore(){
         currentScore += 5;
         score.innerHTML = 'Score: ' + currentScore;
     }
     
     function difficulty(){
         //as the game progresses, increase magnitude of the vertical speed
         if(currentScore % 1000 == 0){
             if(dy > 0)
                 dy += 1;
             else
                 dy -= 1;
         }
     }
     
     function gameOver(){
         //end the game by clearing the timer, modifying the score label
         clearTimeout(timer);
         score.innerHTML += "   Game Over";
         score.style.backgroundColor = 'rgb(128,0,0)';
     }
     
     
 </script>

Bask-A-Bounce

Score: 0

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 rutter · Apr 03, 2012 at 02:51 AM 0
Share

Do both of your objects have colliders attached? Does one or more of them have a rigidbody?

You may want to review Unity's physics manual and check that everything is set up according to spec. I realize that's a pretty general answer, but you're not giving us a lot to work with, here.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Collision 1 Answer

Trouble with raycast hitting a tagged object 0 Answers

Collision isn't detected between two obects 1 Answer

Spawning and health script not working with collider? 1 Answer

How to detect a collision between two certain objects 0 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