Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to make a weight scale?

Discussion in 'Physics' started by Maeslezo, Jul 12, 2017.

  1. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    304
    Hello,

    I want to know how much weight is supporting a collider/rigidbody.
    It can be done somehow, because the wheel colliders know how much weight they are supporting and if you add a cube with a rigidbody above the vehicle, they update its supported weight.

    So I need to know the weight supported by a collider/rigidbody. Any idea?

    Something like this:
    scale weight.png
     
    Last edited: Jul 12, 2017
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    No thread shall remain unanswered :) Maybe you no longer need it but I just had the same question and after a bit of trial and error I came up with a pretty good solution.

    What you have to do is to assign this script to a Rigidbody with a box collider.
    For simplicity, I only used the Y-Axis for measurements.

    Basically, we have to add up all the impulse values of collisions with our weight scale. In fact, it could be optimized even more, by transforming the impulse vector to the weight scale object and only using the force that pushes downwards on it. But for regular use, this will work just fine.

    weightscale.jpg

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class WeightScale : MonoBehaviour
    5. {
    6.     float forceToMass;
    7.  
    8.     public float combinedForce;
    9.     public float calculatedMass;
    10.  
    11.     public int registeredRigidbodies;
    12.  
    13.     Dictionary<Rigidbody, float> impulsePerRigidBody = new Dictionary<Rigidbody, float>();
    14.  
    15.     float currentDeltaTime;
    16.     float lastDeltaTime;
    17.  
    18.     private void Awake()
    19.     {
    20.         forceToMass = 1f / Physics.gravity.magnitude;
    21.     }
    22.  
    23.     void UpdateWeight()
    24.     {
    25.         registeredRigidbodies = impulsePerRigidBody.Count;
    26.         combinedForce = 0;
    27.  
    28.         foreach (var force in impulsePerRigidBody.Values)
    29.         {
    30.             combinedForce += force;
    31.         }
    32.  
    33.         calculatedMass = (float)(combinedForce * forceToMass);
    34.     }
    35.  
    36.     private void FixedUpdate()
    37.     {
    38.         lastDeltaTime = currentDeltaTime;
    39.         currentDeltaTime = Time.deltaTime;
    40.     }
    41.  
    42.     private void OnCollisionStay(Collision collision)
    43.     {
    44.         if (collision.rigidbody != null)
    45.         {
    46.             if (impulsePerRigidBody.ContainsKey(collision.rigidbody))
    47.                 impulsePerRigidBody[collision.rigidbody] = collision.impulse.y / lastDeltaTime;
    48.             else
    49.                 impulsePerRigidBody.Add(collision.rigidbody, collision.impulse.y / lastDeltaTime);
    50.  
    51.             UpdateWeight();
    52.         }
    53.     }
    54.     private void OnCollisionEnter(Collision collision)
    55.     {
    56.         if (collision.rigidbody != null)
    57.         {
    58.             if (impulsePerRigidBody.ContainsKey(collision.rigidbody))
    59.                 impulsePerRigidBody[collision.rigidbody] = collision.impulse.y / lastDeltaTime;
    60.             else
    61.                 impulsePerRigidBody.Add(collision.rigidbody, collision.impulse.y / lastDeltaTime);
    62.  
    63.             UpdateWeight();
    64.         }
    65.     }
    66.     private void OnCollisionExit(Collision collision)
    67.     {
    68.         if (collision.rigidbody != null)
    69.         {
    70.             impulsePerRigidBody.Remove(collision.rigidbody);
    71.             UpdateWeight();
    72.         }
    73.     }
    74. }
     
    dreunin, Gooren, ntgCleaner and 3 others like this.
  3. ntgCleaner

    ntgCleaner

    Joined:
    Jul 9, 2020
    Posts:
    27
    @McDev02 This is amazing! Thank you for sharing this! I've been having some difficulty with some calculations of odd-sized objects with odd center of mass. I was able to use your code above to allow me to set percentages of weight at certain points.

    I am now trying to figure out how I can do this at run time with moving objects! This is a great start, thank you!
     
  4. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    Hi, can you tell me few evidence to understand what it is wrong when using the script.
    If i set only kinématic no change. i have to use gravity....
    Your box collider has 0.2 on Yaxis , why ? not to fill the 3d object ?
    Thanks for the help
     

    Attached Files: