- Home /
How to add spin to a ball in Pong game
So I'm creating a 2D pong game, and I'd like to implement a spin to the ball whenever the player, for example moves down, creating back spin. Or if the player moves up, creating top spin.
And if the ball had some top spin to it, and it hit the other player( who wasn't moving ) then I'd like for it to move in the up direction, opposite with backspin.
Another thing I'd like to implement would be, if the ball had topspin creating, I'd like for it to keep on getting more and more spin if both the players allow it to spin the same direction.
I haven't got much code on this as I have no idea how I would implement this, I've tried messing around with rigidbody2D.rotation
but it doesn't seem to affect much as I'm not completely sure on how to use this.
A game that does this type of physics well, is Sports Heads: Football (This is for reference if someone isn't sure what I mean)
This is the only bit of code I have concerning the player and ball's interaction.
function OnCollisionEnter2D (colInfo : Collision2D) {
if(colInfo.collider.tag == "Player") {
//The velocity of the ball is, the player's velocity/2 + the ball's velocity/3;
rigidbody2D.velocity.y = rigidbody2D.velocity.y/2 + colInfo.collider.rigidbody2D.velocity.y/3;
audio.pitch = Random.Range(0.8f, 1.2f);
audio.Play();
}
}
@$$anonymous$$rSoad I'm very new, I'm not sure how to make that work, what I've tried doing is this:
rigidbody2D.AddTorque (rigidbody2D.velocity.y * 10);
to the OnCollisionEnter2D function shown in my post, but I don't really see a noticeable difference.
@$$anonymous$$rSoad Ah thankyou :) I will mess around with that and see what happens, I'll ask you more questions if/when needed.
Answer by MrSoad · Dec 18, 2014 at 02:21 AM
Take a look at this :
http://docs.unity3d.com/ScriptReference/Rigidbody2D.AddTorque.html
How much effect you get will depend upon several things inc how much friction you get between your ball and your bats.
1)Try out different physical materials on your ball and bats :
http://docs.unity3d.com/Manual/class-PhysicMaterial.html
2) Goto : Edit -> Project Settings -> Physics . Now look at "Max Angular Velocity" which will prob say 7. Up this a lot to 40 or 50 ish, this is the max spin any object can have and 7 will not be enough for what you need.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174313im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using Input.GetAxis on a 2 Player Game 2 Answers
Computer game for 2 players with AUTODETECTION for the gamepads 0 Answers
Help for basic board game but for me it's hard.. 0 Answers
Need help with camera for 2 player roll a ball. 1 Answer
how to make two player bounce off each other but not with the ground? 0 Answers