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 Djaydino · Sep 08, 2012 at 07:27 PM · directionballpongcolission

change ball direction when hitting paddle

hi, i wanted to change the direction from the ball depending where the ball hits on my paddle.

i got it to work kinda but it is affecting the speed from the ball.

here is my code :

 function OnCollisionEnter(ballCollision : Collision)
     {
 
         var p1PosHit : float = ballHit.transform.position.y - paddleP1.transform.position.y;
         var calculateY : float = rigidbody.velocity.y + p1PosHit;
         
         
         //player 1 on collision
         if(ballCollision.gameObject.name == "Player1" )
         {
 
         rigidbody.velocity = Vector3(rigidbody.velocity.x,calculateY,0);
         }


it is a pong clone btw and 2D on x and y (z = 0) and there is no gravity or drag

Comment
Add comment · Show 4
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 Djaydino · Sep 09, 2012 at 08:56 AM 0
Share

and i use bounce

avatar image Djaydino · Sep 09, 2012 at 11:49 AM 0
Share

i think i need to do some thing to get the x and y velocity depending on the magnitude and angle

and then i can add or distract to the angle depending on where the ball hits the paddle

for example : magnitude = 4 and angle = 45 degrees then x = 4 and y = 4

now when i hit my paddle i want to add 20 degrees so it is 65 degrees.

so then the math is : magnitude = 4 and angle = 65 degrees then x = ? and y = ?

so what i need is to get the angle and be able to change it, and a way to calculate this

avatar image fafase · Sep 10, 2012 at 04:15 AM 0
Share

you know what? That is what I gave earlier.

 var norm : Vector3 = other.contacts[0].normal;          
 velocity= velocity- 2 * norm * Vector3.Dot(velocity,       
 norm.normalized);
 velocity.y = 0;   

You have the angle with dot, the magnitude you mention could be the normal or the original velocity (depending what you meant) and the loss of speed is regulate with the normalization of the normal (yeah ok...).

avatar image Djaydino · Sep 10, 2012 at 10:22 PM 0
Share

sorry i am still new to this :)

so how would be the best way to add or distract (for example) 15 degrees from the angle after collision?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Sep 11, 2012 at 06:38 AM

If you want to bounce your ball 15 degrees, that takes about 1 line.

 function OnCollisionEnter(other:Collision){
     if(other.gameObject.tag == "Cube")
         vel = Quaternion.AngleAxis(-15, Vector3.up) * -vel;
 }

This assumes your game is in the x and z direction (flat on the ground) and the y-axis is going out of the screen.

Your vel is the velocity of your ball, Quaternion.AngleAxis takes how much it is supposed to rotate, and the vector on which it should rotate. In our case, -15 and the up axis (y-axis, same things different names). Now we need to invert the ball velocity so we multiply by -vel.

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.AngleAxis.html

I will not go into Quaternion as it is complex number. Just know that it fixes some eulerAngle issues.

Now your problem is that it will always bounce 15 degrees in the same direction.

But considering that your pad is along the x-axis (See pic) (meaning it only moves its position.x) if you subtract the contact position-x with the position.x of the pad, you can know where the ball hit. If the value is <0 then you are on the left you apply -15 and the other way around.

See on the pics, if h is greater than c then you are on the right side. You apply 15 or -15 depending on what you want (the pics shows -15).

Well, all in all I guess that is what you needed now you just use it the way you want.

alt text

The drawing is really bad...


untitled.png (8.3 kB)
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

9 People are following this question.

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

Related Questions

Vector-based Pong-ball bounce calculations 1 Answer

How to change direction of ball whenever i rotate my camera around it? 1 Answer

how to change direction of the moving object? 1 Answer

How to accelerate the ball in a pong clone 1 Answer

Simple Collision Deflection (Pong) 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