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 zhengma38 · Jun 17, 2017 at 04:47 AM · scripting problempong

My ball in pong passes through the paddles.

I'm trying to make a pong duplicate and the ball keeps passing through the paddles. It's slow enough to see go through the paddle. Here is the Ball Movement Script.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class BallMovement : MonoBehaviour {

 public float speed = 10;

 private Rigidbody2D rigidBody;

 //private AudioSource audiosource;

 void Start () {
     rigidBody = GetComponent<Rigidbody2D>();
     rigidBody.velocity = Vector2.right * speed;
 }

 void OnCollisionEnter2D(Collision2D col)
     {
     if ((col.gameObject.name == "Left Paddle") ||
     (col.gameObject.name == "Right Paddle"))
     {
         HandlePaddleHit(col);
     }
     if ((col.gameObject.name == "Wall Top") ||
     (col.gameObject.name == "Wall Bottom"))
     {
         //SoundManager.Instance.PlayOneShot
         //(SoundManager.Instance.wallBloop)
     }
     if ((col.gameObject.name == "Right Goal") ||
     (col.gameObject.name == "Left Goal"))
     {
         //SoundManager.Instance.PlayOneShot
         //(SoundManager.Instance.goalBloop)

         transform.position = new Vector2(0, 0);
     }
 }

 void HandlePaddleHit(Collision2D col)
 {
     float y = BallHitPaddleWhere(transform.position,
         col.transform.position,
         col.collider.bounds.size.y);

     Vector2 dir = new Vector2();

     if(col.gameObject.name == "LeftPaddle")
     {
         dir = new Vector2(1, y).normalized;
     }
     if (col.gameObject.name == "RightPaddle")
     {
         dir = new Vector2(-1, y).normalized;
     }

     rigidBody.velocity = dir * speed;

     //SoundManager.Instance.PlayOneShot
     //(SoundManager.Instance.hitPaddleBloop)
 }

 float BallHitPaddleWhere(Vector2 ball, Vector2 paddle, float paddleHeight)
 {
     return (ball.y - paddle.y) / paddleHeight;
 }

}

If able, please tell me anything wrong with the code.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ChristosKapenis · Jul 03, 2017 at 07:49 PM

Make sure that you have a collider attached on your object and if that's not the case try changing your Rigidbody's collision detection to "Continuous" or "Continuous Dynamic": alt text

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

Answer by TheDJBuntin · Jul 03, 2017 at 07:54 PM

From a similar question I answered:

Have you tried doing it with a lower velocity to make sure it works?

Remember that Update() is ran every FRAME. If there is no frames where the ball is touching the collider there will be no collision. ie. Frame 500 ball is at 500, 0. Target is at 700, 0. But Ball is traveling at 300units per frame. The next time update is called, ball will be at 800, 0.

To solve this, use Continious Collision Detection instead of discrete collision detection. [Christos has provided a screenshot of where to change that.]

Although I've never used it, a quick google also pulled up DontGoThroughThings which uses raycasting to avoid the physics engine allowing fast-moving objects to go through other objects. Here is another thread about this.

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

Answer by Vollmondum · Jul 03, 2017 at 08:34 PM

  1. Untick "Is Trigger" in colliders

  2. One of colliding thingies (a ball or paddle) should have simple shape collider

  3. Open your paddles in 3Ds Max and check Normals. Inverted normals collide from the inside

  4. Check your script with standard Unity cubes

  5. See is your colliders' size coinside with gameObjects sizes. Sometimes those are a dot.

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

103 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

Related Questions

Pong reset state not working correctly 0 Answers

Problem coding script for paddles in Pong without using any physics. 1 Answer

Assembly reference problem (C#) 1 Answer

how can i make a working slingshot or launchpad script 1 Answer

I am using DontDestroyOnLoad Script and when I return to the Menu scene the buttons do not work but the music continues to play 2 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