I have a character with BoxCollider2D
and Animator
components. I need to change a physic material's friction dynamically so I use the next function:
private void ChangeFriction(float friction)
{
boxCollider.sharedMaterial.friction = friction;
boxCollider.enabled = false; // The friction won't be changed if I won't reset the collider
boxCollider.enabled = true;
}
The problem is that after an execution of this function the walking animation isn't played anymore totally. If I comment two last lines then all works perfectly but the friction doesn't change, just like To be or not to be .
How can I fix this issue?
I have been using this piece of code to access friction and change it and it works without enabling and disabling the collider. I can see it changes in Editor as well.
private Collider2D col;
void Start () {
col = gameObject.GetComponent<Collider2D>();
}
void Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
col.sharedMaterial.friction = 1;
}
if (Input.GetKeyDown(KeyCode.Mouse1))
{
col.sharedMaterial.friction = 0.5f;
}
Debug.Log(col.sharedMaterial.friction);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.