Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Simulacr0n · Apr 24, 2018 at 07:18 AM · collisionrigidbodygridfallingsnapping

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
         }
 
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does the object keep falling? 2 Answers

Animator overriding collisions? 0 Answers

Colliding fast moving object with a slow moving object 1 Answer

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

Fixed Joint - Collision Problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges