- Home /
The question is answered, right answer was accepted
objects going through each other
Hi, I am making a beerpong game for a contest(theme is party), but I've been having alot of problems with physics, expecially objects going through each other:
https://dl.dropboxusercontent.com/u/151460612/beerpong/beerpong.html
as you can see cups go through each other, sometimes even the ball goes through the cups, how can I fix it? I think the problem is that the cups are too thin, the thing is, I don't know how to make 'em look good and easy to throw at but not as thin so things don't go through each other, any advice(except making the fixed timestep smaller)? thanks in advance
edit: the collision detection is already continuous dynamic, still doesn't work
edit2: seems that the ball is still going through some cups, I don't know how to fix this-.- the collision detection is already set to continuous dynamic and I'm using the DontGoThroughThings script, also I think the ball isn't that fast :/
Unity's collision detection system is not accurate robust for small or fast moving objects. Check out the "don't go through things" script in the wiki for an alternative approach that works. Basically: Physics.Raycast from previous position to current position in each FixedUpdate.
already tried, didn't worked, btw look at the webplayer, everything isn't that fast, and everything goes through everything
Answer by robertbu · Nov 09, 2013 at 04:37 PM
The issue of the ball going through the cups and the issue of the cups going through each other different. The ball is related to the speed of movement. The ball moves in discrete movements and therefore can miss a collider. It is not a thickness issue. Given what I see in the game, the ball issue can be mitigated/fixed by reducing the the fixedTimeStep. Go to "Edit > Project Settings > Time" and reduce the 'Fixed Timestep' from the default value of 0.02 to 0.01.
As for the cups, in Unity, mesh colliders can only collide against each other if 1) they are marked as convex and 2) if they have less than 255 triangles. The problem is that, for your game, if you make the cups convex, the ball will not go into the cups. Your cup problem is not want I've ever solved, but I have a conjecture on how it might be solved.
Create a child game object to the cup
Create a simplified cup mesh
Create a mesh collider using your simplified mesh
Align the simplified mesh just inside the cup mesh
Mark the mesh as convex
Use Physics.IgnoreCollision() between the ball and all the simplified meshes.
Alternately you could attach a capsule collider to each cup. You will get some inpenetration, but you won't get the cups completely moving through each other.
http://docs.unity3d.com/Documentation/ScriptReference/Physics.IgnoreCollision.html
I just made a new mesh for the cup (to use as collider, 252 Tris, that should work right? :p phew that was close
Okay, I haven't made the Physics.IgnoreCollision step yet, but it was working fine with convex, until I noticed I couln't put the ball inside a cup, maybe because it's totally messing the collider? I don't know if it should be like this but I find it weird so on the left it's the collider with the convex option, and on the right it's not convex, will the last step fix that? I mean, I want to put the ball inside the cup, but the collider won't let me
A simplified mesh will not work by itself since you need to make it convex. If you mark it as convex, then the balls will not go into the cups. So you have to set it up as I've listed above...that is have two mesh colliders one on the parent one on the child. And again, this is just conjecture.
ah I get it now, so basically one mesh collider will collide with all the cups, and the other one will be used so the ball gets inside the cup, but ignores the convex one am I right?
Answer by Penfwasaurus · Nov 09, 2013 at 04:41 PM
Are you sure that the cup are not 'On Triggers' So they fall through anything with a collider?Plus you better check out all the colliders to see whether they are set up correctly, But yeah, also changing the fixed time step can help alot!
Yes I'm sure, and I already said I don't feel like changing fixed time step
Answer by $$anonymous$$ · Nov 09, 2013 at 04:40 PM
The problem is that when the ball is moving too fast, the collisions of the cups and the ball have been skipped since all the collisions in your scene gets calculated in a phisics step. In order to avoid the missed collisions, you need to use raycasting. There is already a script in the Wiki Scripts page here. It uses Raycasting to determine if it will be collided, and will do some complex calculations to avoid clipping or any wired result.
#pragma strict
var layerMask : LayerMask; //make sure we aren't in this layer
var skinWidth : float = 0.1; //probably doesn't need to be changed
private var minimumExtent : float;
private var partialExtent : float;
private var sqrMinimumExtent : float;
private var previousPosition : Vector3;
private var myRigidbody : Rigidbody;
//initialize values
function Awake() {
myRigidbody = rigidbody;
previousPosition = myRigidbody.position;
minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z);
partialExtent = minimumExtent*(1.0 - skinWidth);
sqrMinimumExtent = minimumExtent*minimumExtent;
}
function FixedUpdate() {
//have we moved more than our minimum extent?
var movementThisStep : Vector3 = myRigidbody.position - previousPosition;
var movementSqrMagnitude : float = movementThisStep.sqrMagnitude;
if (movementSqrMagnitude > sqrMinimumExtent) {
var movementMagnitude : float = Mathf.Sqrt(movementSqrMagnitude);
var hitInfo : RaycastHit;
//check for obstructions we might have missed
if (Physics.Raycast(previousPosition, movementThisStep, hitInfo, movementMagnitude, layerMask.value))
myRigidbody.position = hitInfo.point - (movementThisStep/movementMagnitude)*partialExtent;
}
previousPosition = myRigidbody.position;
}
Another case would be if your ball mesh is using a Mesh Collider, and if so I suggest replacing it with a sphere collider.
Follow this Question
Related Questions
Unity Gllitching Everything? 1 Answer
A node in a childnode? 1 Answer
Click and drag - Object go through other objects when dragging 1 Answer
Physics Lagspike? 1 Answer