Skip to content

Instantly share code, notes, and snippets.

@cadenburleson
Last active May 23, 2019 07:32
Show Gist options
  • Save cadenburleson/ddd29015a058f4646fba1d819b728616 to your computer and use it in GitHub Desktop.
Save cadenburleson/ddd29015a058f4646fba1d819b728616 to your computer and use it in GitHub Desktop.
Need help with transforming a collider over time smoothly depending on character height
public CapsuleCollider characterCollider;
public float characterColliderStartHeight;
public float characterColliderEndHeight;
public float colliderTransformTime;
private void Start() {
myRB = this.GetComponent<Rigidbody>() as Rigidbody;
characterCollider = this.GetComponent("Collider") as CapsuleCollider;
characterColliderStartHeight = characterCollider.height;
}
void Update(){
isGrounded();
// Jump
if (Input.GetKeyDown(KeyCode.Space)) {
myRB.AddForce(0,jumpHeight,0, ForceMode.Impulse);
anim.SetTrigger("Jump");
jumpEvent.Invoke();
}
}
//public float distanceToGround;
//public bool grounded;
//public Vector3 hitPointInfo;
public void isGrounded(){
RaycastHit hit;
Ray groundedRay = new Ray(transform.position, -Vector3.up);
// if (Physics.Raycast(groundedRay, out hit, distanceToGround + 0.1f)) {
if (Physics.Raycast(groundedRay, out hit, Mathf.Infinity)) {
distanceToGround = hit.distance;
if (hit.distance > 3.0f) {
Debug.Log("hit distance is greater than or equal to 3.0");
//Decrease the collider size
characterCollider.height = Mathf.Lerp(characterColliderStartHeight, characterColliderEndHeight, colliderTransformTime * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment