- Home /
Rolling a ball in 2D, in direction of movement
Hi all, I currently have an overhead 2D game where the player can kick a ball around. I've managed to get the ball to move in the direction it was kicked, but making it rotate to give the illusion of rolling seems to be eluding me. Right now I have this:
Vector3 v = (transform.position - c.transform.position);
rigidbody.AddForce(v * hardPush);
rigidbody.AddTorque(-v * hardPush);
The addForce bit kicks the ball and works fine enough. Addtorque rotates the ball, but it doesn't look quite right. The texture doesn't move around as it should. Does anyone have any tips to simulate believable ball rolling? Thanks!
have you tried with transform.rotate when its kicked? you can give a rotation speed based on the real speed of the ball, when its going fast will rotate fast when its going slow will rotate slowly.
This SEE$$anonymous$$S to have worked pretty well! The only problem is that the ball is rotating the wrong way. I guess I'll tinker about and see if I can fix it.
you can write a funciton to check if the ball its kicked to the right or the to left and based on that give positive or negative values to z.
I ended up just inputting the z and y velocity values in the reverse positions in the rotate function (i.e. transform.Rotate(velocity.y, velocity.x, velocity.z). It seems to work perfectly now!
Answer by saravanan-P · Jun 08, 2015 at 09:33 PM
use this code...
GetComponent.().MovePosition(GetComponent.().position + speed * Time.deltaTime);
And for speed variable: var speed : Vector2 = Vector2 (12, 0);
or use this code:
this.GetComponent.().AddForce(Vector2(-1,0) * 12);
Your answer
![](https://koobas.hobune.stream/wayback/20220613182706im_/https://answers.unity.com/themes/thub/images/avi.jpg)