- Home /
Can I implement basic physics on cubes without writing code?
So I'm creating a game that uses cubes as the meshes for everything. I will relay all the detail through textures (animated when needed). The game is so simple I dont really even need gravity or forces etc. I really just need to keep to cubes from intersecting. Is there a way to do this with the existing rigidbody/collision mesh without too much coding?
Edit: For clarity, this is how I'm implementing things. The player and all environment objects have cube collision meshes and rigidbodies. I'm affecting the transforms of the player to move him around. Note: rigidbody.MovePosition works as well. Its what I have in place now but it doesn't seem to make a difference. When the player hits say a table (whose collision mesh is about half the height of the player) the player goes flying off into space. This is not the kind of physics I'm looking for. I don't need authentic gravity or rotational forces. I want the cubes to always be oriented the same way and to stay on the ground (I guess that I would just attempt moving the player down x units each frame to simulate gravity). All I need from the physics engine is to prevent objects from overlapping.
Here's another way of putting it: I want to the physics engine to limit motion rather than have it attempt to move things in response.
I know how to write things like this in 2D, and I could probably do it for my game, but then, that's the sort of reason I wanted to use Unity in the first place (but in relation to Networking rather than physics.)
Use "iskinematic" property of rigidbody to prevent forces and gravity. Can you explain what type of behaviour you want in your basic physics?
if the cubes have box colliders (and rigidbodies, I think) attached to them, they will collide with each other and not intersect.
Domain: Yes this is true but that implementation brings with it a lot of other interactions that I don't want.
Ashish: I'm editing the post to make it more clear.
Ok, here's what I've done. If theres a better way of doing this please tell me, because its effectively restricting my game to a 2D environment.
I put colliders on all objects. On the players, I put a rigidbody with constraints on Y motion, and XYZ rotation. It gets the job done, but I'd like to be able to have a 3D and this isn't particularly conducive to vertical motion.
I'm not 100% on this! but you don't want rigid bodies. Just attach colliders to the objects in the world. You only need one rigid body, the player. If you update the transform position manually, the physics won't kick in to limit your position. So you'll need to move your player through the rigidbody interface. You can modify the physics parameters for the scene through the Edit, project settings, physics menu. You might be able to tweak the values (for instance turn gravity off, set bounciness to 0, etc).
Answer by nesis · Feb 27, 2014 at 03:17 AM
You want Colliders (in your case, BoxCollider) with no Rigidbody on objects that you want to collide with, but not push around.
You want a Rigidbody and a Collider (in your case, BoxCollider is fine) on your player. For the Rigidbody, freeze rotation. To move the player, you should be able to use rigidbody.MovePosition(transform.position + someVelocity*Time.fixedDeltaTime)
in a FixedUpdate()
method in a script attached to your player. You want to use FixedUpdate() for moving objects, since that's the update method used for physics (since it runs at fixed intervals, and occasionally runs multiple times per frame to keep up with the Engine's physics update loop).
Flying off into space as you were saying would only happen if your player's collider ended up intersecting the other collider, perhaps from using transform.Translate() instead of rigidbody.MovePosition(). rigidbody.MovePosition() should never result in intersections with colliders that don't have rigidbodies, as far as I know.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133241im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Character Controller Platformer - Hang on to ledges 0 Answers
OnCollisionEnter isnt called when player lands on object 1 Answer
Collision with rigidbody inside a gameobject 0 Answers
Best way to do smooth object movement and obstacle detection+animation reset ? 0 Answers
Did triggers inside loaded gameobjects break in a recent update? 0 Answers