- Home /
Moving a ball through add force and rotate around
Stop be if you've heard this one before. I have a Rigidbody ball move along the outside of a tube. The script is pretty simple first pull the ball towards the tube, second add force to move the ball forward. Last get user input to rotate the ball around the tube. The problem is When the ball rotates around the tube too much too fast, the rotation gets out of whack and the ball goes flying into space. This is odd for a number of reasons, mostly because I have the balls rotation frozen in the Rigidbody inspector. Here's the script, can you help me?
function FixedUpdate() {
rigidbody.AddForce((-transform.up)*11);//Gravity
rigidbody.AddForce(transform.forward*5);//Forward
Physics.Raycast(transform.position, -transform.up, hit, .5);//Begining of Turn
rot = Input.GetAxis("Horizontal") * -10;
if(rot != null && hit != null){
Debug.DrawRay(hit.transform.position, (hit.transform.up), Color.white, 20);
transform.RotateAround(hit.transform.position, Vector3.forward, rot);
}
}
Answer by robertbu · Jan 11, 2013 at 08:43 AM
Some suggestions.
Try upping the "Solver Iteration Count" (Edit/Project Settings/Physics). "Frozen" and/or "Locked" seems like more of a guideline to the physics engine in my limited experience, but upping the count helped me when I had frozen issues.
Limit the maximum velocity. In FixedUpdate() you can add something like:
if (rigid_body.velocity.magnitude > 10.0f)
rigid_body.velocity = rigid_body.velocity.normalized * 10.0f;
Given the regular nature of your geometry, you could even reposition your sphere and reset the velocity vector if the sphere goes out of bounds.
Or you could add an invisible tube with a collider so that the ball is really traveling between the inner tube and outer tube.
While I'm sure you're solution would have worked I actually figured out what the problem was, and still is, the ball is rolling on a curved tube right now, so the curve is forcing the ball to rotate around its z axis strangely. And when it did that the -transform.up, would pull it away so I've replaced that gravity with rigidbody.AddForce((tube.position-transform.position).normalized * 11); That seems to work but it is still rotating weird when I move it.
Your answer
Follow this Question
Related Questions
How to move a rigidbody along a curve or spiral 0 Answers
Jumping with rigid body3d 1 Answer
rolling ball movement on different axis 0 Answers
push crate - works in editor, not on device 2 Answers
Rotation problem with spawning bullet 2 Answers