• 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 MRIT · Jan 19, 2012 at 03:03 PM · collisionphysicsballpongpaddle

Simple Collision Deflection (Pong)

Hi,

I am working on my first Unity game, a Pong clone.

I have utilised some of the in built features to get the main game working although I am having trouble with the ball deflecting at an angle off the paddle unless it $$anonymous$$ts the edge. When it $$anonymous$$ts the flat face of the object it just deflects horizontally across the x plane.

The ball is a rigidbody w$$anonymous$$ch $$anonymous$$ts box colliders on the paddles. The ball has one script and the verticalcontroller script manages the paddles.

Also there is no gravity, angular drag or friction enabled.

 using UnityEngine;
 using System.Collections;
 
 public class Ball : MonoBehaviour {
 
     private const float velocityIncrement = 1.0f;
     
     void Start() {
         
         Reset();    
     }
     
     void OnTriggerEnter(){
         Reset();
     }
     
     void Update()
     {        
         //rigidbody.AddForce(10, 10, 0, ForceMode.Impulse);
         rigidbody.velocity *= velocityIncrement;
     }
     
     private void Reset()
     {
         transform.position = Vector3.zero;
         rigidbody.velocity = new Vector3 (80,1,0);
         //rigidbody.angularVelocity = new Vector3 (0,10,0);
     }
         
     
 }

 using UnityEngine;
 using System.Collections;
 
 public class VerticalController : MonoBehaviour {
 
     public string axisName = "Vertical";
     public float speed = .5f;
     
     // Update is called once per frame
     void Update () {
     
         var delta = new Vector3(0,speed,0);
         
         if (Input.GetAxis(axisName) >= .001)
             delta *= 1f;
         else if (Input.GetAxis(axisName) <= -.001)
             delta *= -1f;
         else
             delta = Vector3.zero;
         
                       
         transform.position += delta;
     
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tyler 10 · Jan 19, 2012 at 05:48 PM

You should check out these functions:

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnCollisionEnter.html http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionExit.html

Once you know your ball has collided with your paddle, you can add a force based on the balls distance to the center of the paddle. That way, if its above the center of the paddle it will make the ball go up, and if its below, it will make the ball go down.

It would look somet$$anonymous$$ng like t$$anonymous$$s

function OnCollisionEnter(collision : Collision) { var ball:Rigidbody = collision.rigidbody; if(ball.transform.y > transform.y){ ball.addforce(. . . . .) . . . . } }

Comment
Add comment · Show 6 · 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 MRIT · Jan 19, 2012 at 09:16 PM 0
Share
avatar image Tyler 10 · Jan 19, 2012 at 10:52 PM 0
Share
avatar image MRIT · Jan 19, 2012 at 11:42 PM 0
Share
avatar image Tyler 10 · Jan 20, 2012 at 01:12 AM 0
Share
avatar image MRIT · Jan 21, 2012 at 12:53 AM 0
Share
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy bouncing physics 0 Answers

Vector-based Pong-ball bounce calculations 1 Answer

Ball jump on collision problem 1 Answer

Scoring not working 2 Answers

Moving a ball around a maze 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