- Home /
Dynamic collider when jumping.
I have this jump animation that "crouches" the model when jumping. Now, this makes the feet deceptive, because if the feet would appear to land just on top of a ledge, the collider box would hit the side of said ledge by a large margin.
This is my solution to the problem:
coll = GetComponent<CapsuleCollider>();
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
coll.center = new Vector3(0.0f, 1.4f, 0.0f);
coll.height = 1.5f;
isGrounded = false;
rb.AddForce(new Vector3(0.0f, jumpHeight, 0.0f));
anim.SetTrigger("isJumping");
}
void OnCollisionEnter()
{
isGrounded = true;
coll.center = new Vector3(0.0f, 0.86f, 0.0f);
coll.height = 1.9f;
}
(Both the input part and getcomponent are both in their corresponding functions, and "coll" is defined above, I just set it up like this for relevancy. This does "work")
Now, the issue here is apparent. When jumping, the collider becomes smaller, which means that when the character model lands, the collider is far above its feet, causing the character to sink into the floor before the collider is reset. The question is if there is an alternate solution to this, a fix, or if I just have to add another hitbox (which is just plan B, I wouldn't want this if there is another way.)
as a sidenote, I'm trying to integrate double jump into my existing functions, so if it's not too much trouble, I'd appreciate it if you could include that in the answer
You can just use RayCast for detecting ground below for expanding collider before landing, something like this :
private void Update() {
if(!isGrounded && Physics.Raycast(new Ray(playerFeetPositionVector,Vector3.down),DistanceForFeetControl))
{
isGrounded = true;
coll.center = new Vector3(0.0f, 0.86f, 0.0f);
coll.height = 1.9f;
}
}
please note that this is just pseudo code
Answer by ASPePeX · Dec 07, 2017 at 03:14 PM
The "nice" way of doing it would be to fix the animation so the transform origin is always the the characters feet.
Your answer
![](https://koobas.hobune.stream/wayback/20220612144149im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player sticks to ground occasionally after trying to jump 0 Answers
Jumping on top of an enemy problem 1 Answer
Can't figure out how to detect ground (2D) 1 Answer
Box collider didn't jump with player 1 Answer
Detecting Collision for Jumping 1 Answer