- Home /
Ball physics help
Hello everyone.
I'm trying to make a simulation of an object but I'm having a lot of trouble getting it working.
Let me explain:
Think of the black as a transparent plastic ball.
In the center of the ball, there is a cylinder which is connected to the ball on both sides (blue).
On the center of the cylinder, there is a box (green) with a hole for the cylinder to go trough.
Most of the box's weight is at it's buttom (red).
Here is how the ball needs to behave:
When going forward, the ball needs to rotate forward while the box needs to stay the same (because the weight keeps it down).
Same thing for going back (ball rotates back and box stays).
Conclusion:
Because I'm trying to simulate it, it needs to be done with physics and not animation or anything like that.
I tried playing around with rigidbodys and joints but without getting the desired results.
Any help on how to build this object and how to move it around will be really appreciated.
Thanks!
Can't you just restrict the rotation of the blue cylinder and green box in the rigidbody constraints? Unless there is something else you aren't saying that you need that should keep the box in the center and the ball can go wherever it wants.
You might have to play around with the child/parenting of the objects, to let the box take the balls position, but the box shouldn't rotate with it.
The box needs to be able to rotate but it's weight will keep it almost not moving.
Remember, this is a simulation, I'm trying to get it to be as real as possible.
Let me try to break it down to parts.
Right now I have a ball.
The ball has a rigidbody with sphere collider.
I'm trying to use this code to move it:
void FixedUpdate() {
if(Input.GetButton("BallForward")){
power = 10
rigidbody.AddRelativeForce(transform.forward * power);
}
if(Input.GetButton("BallBackward")){
power = -10;
rigidbody.AddRelativeForce(transform.forward * power);
}
}
But for some reason, the ball starts moving forward and then its going back (while I still have the key pressed) like something is trying to hold it in place.
Ok, nvm I solved the problem.
It was very weird.
If the script was on the object, it didn't work.
The second I put the script on a different object, it worked :S
Now for another part:
I managed to attached the center box(green) to the bar in the middle(blue) but I'm not sure what's the best way to create the weight.
Can you put an empty/non-rendered game object on the bottom of the box and add mass? Connect it the same way you did with the box to bar.
Answer by ExpiredIndexCard · Apr 01, 2013 at 10:55 PM
Make the square kinematic. If that doesnt work, then use a Physic Material. Add the physic material to your ball and square. Maybe increase the mass of the square. If you need help with physic materials, check this out: http://unity3d.com/learn/tutorials/modules/beginner/physics/assignments/bouncing-ball http://docs.unity3d.com/Documentation/Components/class-PhysicMaterial.html
Answer by Owen-Reynolds · Apr 06, 2013 at 03:35 PM
To put a "weight" on the bottom of the interior cube without a new object, can use rigidbody.centerOfMass = new Vector3(0,-3,0);
The center of mass doesn't even have to be inside the object. The lower it is, the more the square will stay up like a punching bag clown.
Thanks, that's good to know.
I still have the problem of the object swinging from side to side (when I'm pressing any keys).