- Home /
Snapping falling objects with rigidbody to grid
In my game the player can remove and place blocks in a grid, similar to minecraft. Each block has a fixed position. I want to have blocks that are able to fall. At the moment I'm using a rigidbody. The rotation of the rigidbody is locked on all three axis and the position is only unlocked in y-direction so the object can only fall straight down. My problem is to align the block perfectly when it is landed. The code I tried is able to snap a block into position after it has landed, but the problem starts when a second falling block lands on top of the other one. They both seem to push themself away a tiny amount (numbers with 10^-8), but I don't feel comfortable to leave them in these positions as this could interfere with future block placement. Heres my code so far that snaps the blocks on grid. Each falling block has this script attached together with a rigidbody. The colliders of the block are exactly the size of the grid.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallingBlock : MonoBehaviour {
Rigidbody rb;
public int gridScale = 3; //The size of each block
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
if (Mathf.Approximately(rb.velocity.y, 0f)) //Check if velocity is zero (or almost zero)
{
rb.velocity = Vector3.zero; //Set velocity to zero, so the block doesn't move on
Vector3 pos = transform.position / gridScale; //Divide current position by gridsize
Vector3 placePosition = new Vector3(Mathf.Round(pos.x), Mathf.Round(pos.y), Mathf.Round(pos.z)); //Round all coordiantes, so the block can get sanpped to the grid
placePosition = placePosition * gridScale; //Multiply the coordiantes again
transform.position = placePosition; //Set the position of the obejct
}
}
}
Answer by Simulacr0n · Apr 24, 2018 at 07:33 AM
I apologize for my thread. After a bit of trial and error I got it to work. The solution was to enable and disable the isKinematic option. Here's the code if anybody in the future will have this problem. Note that the code is probably far from perfect and probably hides his own problems:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallingBlock : MonoBehaviour {
Rigidbody rb;
public int gridScale = 3;
int placeMask;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
int layerMask = 1 << 8;
placeMask = layerMask;
placeMask = ~placeMask; //Only Ignores Colliders, but not the player (Layer 8 is for colliders that should be ignored
}
// Update is called once per frame
void FixedUpdate () {
if (Mathf.Approximately(rb.velocity.y, 0f))
{
rb.isKinematic = true;
Vector3 pos = transform.position / gridScale;
Vector3 placePosition = new Vector3(Mathf.Round(pos.x), Mathf.Round(pos.y), Mathf.Round(pos.z));
placePosition = placePosition * gridScale;
transform.position = placePosition;
}
Collider[] hitColliders = Physics.OverlapBox(new Vector3(transform.position.x,transform.position.y-gridScale,transform.position.z), new Vector3(gridScale / 2, gridScale / 2, gridScale / 2), Quaternion.Euler(Vector3.zero), placeMask);
if (hitColliders.Length == 0)
{
rb.isKinematic = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612161419im_/https://answers.unity.com/themes/thub/images/avi.jpg)