- Home /
shrinking objects
So this script makes the gameobject shrink at a rate i chose, along with the mass. but the problem is no matter how much mass i give an object, it will loose mass at the same speed. so like if i gave an object a mass of 10 and another object a mass of 100 , they would end up with a mass of 0 and a size of 0 at the same time. so what i want is if two objects both the same size but one with more mass . the one with more mass will shrink slower than the one with less mass
private Rigidbody rb;
private float originalMass;
private Vector3 originalScale;
private float counter;
public float timeToShrink;
private Vector3 someVector;
private bool hasEntered;
float shrinkFactor;
void Start()
{
rb = GetComponent<Rigidbody>();
originalMass = rb.mass;
originalScale = transform.localScale;
}
void ShrinkBabyShrink(float shrinkFactor, Vector3 targetScale, float targetMass)
{
Vector3 newScale = Vector3.Lerp(originalScale, targetScale, shrinkFactor);
float newMass = Mathf.Lerp(originalMass, targetMass, shrinkFactor);
transform.localScale = newScale;
rb.mass = newMass;
}
void Update()
{
if (hasEntered && shrinkFactor <= 1)
{
counter += Time.deltaTime;
float shrinkFactor = counter / timeToShrink;
ShrinkBabyShrink(shrinkFactor, someVector, 3);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Death")
{
hasEntered = true;
}
}
private void OnTriggerExit(Collider other)
{
hasEntered = false;
}
Answer by mayur7garg · Jul 12, 2018 at 09:03 AM
Try dividing the shrinkFactor by the original mass in the Update loop.
float shrinkFactor = counter/(timeToShrink*originalMass);
This way the shrinkFactor will take a longer time to reach its maximum value (1) for heavier objects as compared to lighter objects. But be sure that shrinkFactor <=1 before passing it to the ShrinkBabyShrink function which can happen for some values of originalMass less than 1.