Reputation: 31
I am creating a player jump function and the collider does not achieve the same height as my players feet on jump.
I am currently trying to manually position the collider based on the position of the player. However, this has caused the capsule to now go way too high and because of the time it takes to fall, the player ends up halfway submerged in the ground.
void Jump() {
if (Input.GetKeyDown(KeyCode.Space)&& isGrounded == true) {
isGrounded = false;
actions.Jump();
rigidbody.AddRelativeForce(Vector3.up * jumpSpeed);
startTime = Time.time;
}
else {
if (transform.position.y < .6f) {
isGrounded = true;
}
}
}
private void Update() {
if (!isGrounded) {
dist = (Time.time - startTime) * capSpeed;
fracOfJourney = dist / journeyLength;
capCollider.center = Vector3.Lerp(start, end, fracOfJourney);
}
else {
// capCollider.center = Vector3.Lerp(end, start, fracOfJourney);
}
}
Upvotes: 1
Views: 982
Reputation: 183
Rather than calculating the capsule collider position yourself a better solution would be to separate the rendering components of your player from the capsule collider. As a possible solution, you could have in your heirarchy:
Player Root: (rigidbody, player animator, Movement Script) Renderer: (Mesh of player and renderer here) Collider: (Capsule Colider here)
After doing this you could match the boundaries of your capsule collider by changing the scale and position as needed during your jumping animation. Separating the collider into it's own gameobject allows you to change the scale without effecting the appearance or position of the player.
Upvotes: 1