- Home /
[SOLVED] Desactivate pushing forces between two objects
Hi, I am currently making a 3D Tetris-like game, and I wonder if I can prevent a object to push another when they collide.
For exemple :
A is a piece already set and B the new one the player is placing.
I want B to be block by A but not pushing it.
B is kinematic and A is dynamic, I can't pass A in kinematic I need it to keep its physics.
For the moment B is a trigger so it pass through A, I want that A works like a wall for B but in the same time it has to keep its physics.
B is the moving piece there, A is the tower made by all the pieces already placed.
Solution : I used a sweepTest.
Answer by lgarczyn · Nov 14, 2019 at 08:04 PM
So if I understand correctly, you want to be able to move an object to place it, but not interact with the scene until it is dropped, right? Let's call the cube being placed A, and every other cube B.
The best way would be to disable the rigidbody of A during placement in some way (set it as trigger, turn off collisions, put it in another layer or disable the collider), and use a boxcast to verify that you are never passing through any B block.
Edit: for a mesh, a boxcast on the bounding box or a SweepTest on the collider would work.
Another way, that is very much a cheat, but would work with minimal effort if (and only if) you are using velocities or forces to move A, would be to set the weight of A very low, and of B very high.
Another way would be to set every B as kinematic during placement. Again, only works if you use velocities and not MovePosition or setting A's position directly.
The problem is that what you want is called a one-way interaction, and is only possible in Unity through kinematic objects vs dynamic. The only real way to have two dynamic objects interact is to put them in different layers, and mirror the action of the interacter (B) in the interactee (A) layer using a kinematic body. This could also work for you.
Answer by Nylash · Nov 15, 2019 at 01:59 PM
The boxcast is a good idea but in the end, we will get various pieces and not simple cube or rect it could be a boat or a hat, and then the boxcast will not be precise enough.
" The problem is that what you want is called a one-way interaction, and is only possible in Unity through kinematic objects vs dynamic. The only real way to have two dynamic objects interact is to put them in different layers, and mirror the action of the interacter (B) in the interactee (A) layer using a kinematic body. This could also work for you."
Actually it's what I got here. But I control the kinematic one and it's the problem I think, but I can't change that. If the piece I control is not kinematic it does strange thing and if all the pieces already placed are not dynamic I loose the concept of the game :/
For move my object I use transform.Translate to move it on a plane parallel to the camera.
Using transform.Translate on a rigidbody is bad and you should feel bad.
To move a rigidbody as dynamic, set the velocity to your input each frame. If you use a visible cursor, you can use smoothdamp to do that.
To move a kinematic rigidbody, use rigidbody.$$anonymous$$ovePosition.
If every other object is kinematic, you're effectively pausing the game, is that so bad? Otherwise a block B could fall on the block A you are trying to place, which would make it fly around as it tries to avoid merging through, while also not being able to affect the falling block B.
Anyway just replace the boxcast by a SweepTest
I will use rigidbody.$$anonymous$$ovePosition, sorry :(
Yeah, actually we want the tower to keep its physics otherwise it could cut its falling.
Hell, didn't know this thing ! It could totally do the work, I will try it !
Your answer
Follow this Question
Related Questions
Child Rigid Body collision problem 2 Answers
Realistic collisions with a kinematic rigidbody? 1 Answer
Preventing objects from intersecting with minimal physics 3 Answers
Disable rigidbody from being able to move other rigidbodies 1 Answer
Are my kinematic rigidbodies slowing my scene down? 2 Answers