Blendshapes

Hi, i want to make a car damage by using blendshapes, i only use 2 key the main key and the destroyed key.

The blendshape goes from 0 to 100 and i want a script to use this blendshape for my dmg on the car but if my car is dmg by 20 i want blendshape to go to 20 and if its dmg by another 30 i want blendshape to go to 50.

Is this posible thru scripting, i tried search for it but cant find it.

Thank you

Hi,
Have you read this manual page:

You can use SetBlendShapeWeight if I remember correctly.

But there it seems the whole blendshape will be played, i only want a litle of it to be played based on the damage the car has taken so if dmg of car is 20 i want blendshape set to 20. or what would be good is if i can set the blendshape variable to the number my car has in dmg, the proble is just how to change the blendshape, i cant understand it.

Blend shapes do not get played unless you actually make an animation that changes them over time. SetBlendShapeWeight sets the weight instantly to the value you specify and leaves it there.

1 Like

so does that mean i can change the value instantly with SetBlendShapeWeight and the object will change form instantly or is it imposible to change the object shape based on number value?

Yes.

1 Like

Thank you for your answer, i mangage to just make a quick script:’

using UnityEngine;

public class TestShapeKey : MonoBehaviour {

    public float value;
    public SkinnedMeshRenderer mySwitchMesh;

    void Start()
    {
        mySwitchMesh = GetComponent<SkinnedMeshRenderer>();
    }

    public void TestButton()
    {
        Damage();
    }

    public void Damage()
    {
        mySwitchMesh.SetBlendShapeWeight(0, value);
    }

}

this now works what i set value on and click button, so i can make that into car damage just replace the value with CarDamageValue, but is it posible to make it go smooth and not change instantly?

Not by default, so you need to do it yourself with something like this:

private float _TargetWeight;
private float _Speed = 1;

public void Damage()
{
    _TargetWeight = value;
}

private void Update()
{
    var weight = mySwitchMesh.GetBlendShapeWeight(0);
    weight = Mathf.MoveTowards(weight, _TargetWeight, Time.deltaTime * _Speed);
    mySwitchMesh.SetBlendShapeWeight(0, weight);
}
1 Like

so i tried this and it seems to work:

using UnityEngine;

public class TestShapeKey : MonoBehaviour {

    public float value;
    public SkinnedMeshRenderer mySwitchMesh;
    public int maxCarDamage = 1000;
    public int currentCarDamage = 0;
    private float _TargetWeight;
    private float _Speed = 100;

    void Start()
    {
        mySwitchMesh = GetComponent<SkinnedMeshRenderer>();
    }

    public void TestButton()
    {
        Damage();
    }

    public void Damage()
    {
        value = currentCarDamage * 100 / maxCarDamage;
        _TargetWeight = value;
    }

    private void Update()
    {
        var weight = mySwitchMesh.GetBlendShapeWeight(0);
        weight = Mathf.MoveTowards(weight, _TargetWeight, Time.deltaTime * _Speed);
        mySwitchMesh.SetBlendShapeWeight(0, weight);
    }

}

Thank you again so much now when i change car damage it make the number down between 0-100, so it fits the BlendShape value. But just a quick question the “var weight” what does that mean the var?

“var” lets it automatically determine what type of variable to use. GetBlendShapeWeight returns a float, so weight will be a float.

1 Like