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);
}
}
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.