Assiging character controllers capsule collider to cloth?
So I'm using the Unity character controller component, which obviously has its own capsule collider element to it. I want to assign this to cloth so that the player character can interact with said cloth without the need for more colliders getting added. I am really struggling to figure out how to properly do this though.
It seems like it should be possible, maybe I am just being dumb.
Answer by Graithen · Jun 19, 2019 at 05:47 PM
Given the apparent limitations of the character controller component, I remade my player character using a rigid body based solution. I then add the players capsule collider to the cloth at runtime by getting it with a trigger and adding it to the cloths array of capsule colliders.
This allows players to affect cloth client side in multiplayer.
Answer by ColtPtrHun · Dec 30, 2021 at 09:17 PM
Hi there! I'm facing the same problem. I came up with these workaround:
I created an empty child gameObject under the player and named it "CapsuleDuplicate".
I added a capsule collider component and copied the values from my character controller. (Also increased the radius a bit to avoid clipping through the cloth.)
I changed the Layer of this gameObject to a new one. Under Edit/Project Settings/Physics/Layer Collision Matrix, I deselected everything for this layer, so it won't collide with the world, nor the capsule from the character controller itself.
In my case, since the cloth and the player is located in different scenes (Gameplay & Level) the plot goes on:
I wrote a simple script and attached to the cloth object. This searches for the capsule duplicate and attaches it to the colliders of the cloth:
using UnityEngine;
public class Curtain : MonoBehaviour
{
void OnEnable()
{
CapsuleCollider[] capsules = new CapsuleCollider[1];
capsules[0] = GameObject.Find("Player").transform.Find("CapsuleDuplicate").GetComponent();
gameObject.GetComponent().capsuleColliders = capsules;
}
}
The comment of @methos5k under https://forum.unity.com/threads/adding-colliders-to-a-cloth-component-using-script.512879/ helped me during this.