- Home /
pong rolling ball
hi , i'm still young at unity and java and my first project is a pong like game (2d)
i got everything working except my ball is not rolling.
it is moving but when i added my ball texture i noticed it is not "rolling"
i use this to start moving my ball :
gameObject.rigidbody.AddForce(200,randomAngle,0);
Then to speed up the ball i use :
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name == "Player1")
{
gameObject.rigidbody.AddForce(rigidbody.velocity.normalized * 55);
audio.PlayOneShot(paddleSound1);
}
if(theCollision.gameObject.name == "Player2")
{
gameObject.rigidbody.AddForce(rigidbody.velocity.normalized * 55);
audio.PlayOneShot(paddleSound2);
}
there is no gravity and its 2d.
i think i have to get the x and y direction + the velocity to give it to the rotation but i don't know where to start atm, and maybe there is an easier way to do that.
thanks in advance for helping :)
Answer by nikescar · Sep 02, 2012 at 10:31 AM
The easiest way to do this would be to actually have the ball roll on a surface with a certain friction. Add a plane and enable gravity. If you want to calculate the correct rolling rotation without using the built-in physics that would do this for you, then you are in for a lot of fun/frustration.
I guess my follow-up question would be why do you want to do it without the physics engine?
my whole game is on side view atm and so i would have to turn everything to top view .
the reason is i that i want to make a kind of random rotating effect on it, for example to give it extra top spin, side spin or other effects.
i found how to get the velocity , x and y :
var testDisplayVel : GUIText;
var testDisplayX : GUIText;
var testDisplayY : GUIText;
function Update () {
var ballSpeed = rigidbody.velocity.magnitude;
var xDirection = rigidbody.velocity.x;
var yDirection = rigidbody.velocity.y;
testDisplayVel.text = ballSpeed + "speed";
testDisplayX.text = xDirection + "x";
testDisplayY.text = yDirection + "y";
}
i think i will manage to get my ball rolling now, thx for the help anyway. if i got it rolling good i put my script here if someone else needs it :)
my ball is rolling now But not @ right direction i did this :
function Update () { var ballSpeed = rigidbody.velocity.magnitude; var xDirection = rigidbody.velocity.x; var yDirection = rigidbody.velocity.y;
transform.Rotate(yDirection ,-xDirection , 0); }
when i only use :
transform.Rotate(0 ,-xDirection , 0);
it rolls good in the x direction, and when i use :
transform.Rotate(0 ,-xDirection , 0);
it rolls good in the y direction, But when i use both it starts good but after collition it goed into strange directions
Your answer
Follow this Question
Related Questions
How to roll a ball ? 2 Answers
Making a ball roll and keeping the axis? 2 Answers
Add tilt function to rotate a ball android device 0 Answers
Rolling a ball in 2D, in direction of movement 1 Answer
Stopping rotation at 90° and -90°/270°. 0 Answers