- Home /
Deform Charater controler Collider acording to stance
I've been messing around with character controller and it's colliders
The idea is to deform the collision on the character when the characters changes stance (i.e stand, crouch, Prone etc.)
I've tried adding different colliders to the characters bones. but the character also makes use locomotion, so the rays casted from locomotion starts clashing with the colliders.
I've also tried scripting to the character controller to change size when the character changes stance, but with no results.
Any suggestion is appreciated. :)
Answer by Cyb3rManiak · Apr 21, 2011 at 12:55 PM
I don't know if it will play well with locomotion or not, but I've tried it, and scripting the character controller collider DOES work... At least changing the height and center of the collider.
I created a small example project, but can't upload it here, so if you need it send me your email using a private message or something.
The code attached to my character controller:
using UnityEngine; using System.Collections;
public class Crouch : MonoBehaviour { public Transform tHead;
private Vector3 _v3InitialPosition;
private Vector3 _v3CrouchingPosition;
private CharacterController _characterController;
private float _fInitialControllerHeight;
private float _fCrouchingControllerHeight;
private Vector3 _v3InitialControllerCenter;
private Vector3 _v3CrouchingControllerCenter;
void Start()
{
_v3InitialPosition = tHead.localPosition;
_v3CrouchingPosition = _v3InitialPosition + Vector3.down;
_characterController = GetComponent(typeof(CharacterController)) as CharacterController;
_fInitialControllerHeight = _characterController.height;
_fCrouchingControllerHeight = _fInitialControllerHeight - 1f;
_v3InitialControllerCenter = _characterController.center;
_v3CrouchingControllerCenter = _v3InitialControllerCenter + Vector3.down * 0.5f;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
tHead.localPosition = _v3CrouchingPosition;
_characterController.height = _fCrouchingControllerHeight;
_characterController.center = _v3CrouchingControllerCenter;
}
if (Input.GetKeyUp(KeyCode.Space))
{
tHead.localPosition = _v3InitialPosition;
_characterController.height = _fInitialControllerHeight;
_characterController.center = _v3InitialControllerCenter;
}
}
}
Thanks your answer, i've tried this now and it's works, but when ever i apply it my character ended up falling throw the floor.
But I don't need this function any more as i started adding Collision boxes as Triggers ins$$anonymous$$d(and shrinking the Character controller) which solved most of the problems i had with the Colliders.
Thanks anyways.