- Home /
How to jump through box colliders but jump on top of them
I need help with a script. I'm currently working on a jumping game and I need help with one part. The character is jumping from platform to platform fine but when he is going more up and comes to a place the platforms are almost blocking the way up he is not able to jump through the colliders. How can I make this happen? For an example see: Sonic jump, Doodle jump etc.
This is my script.
public float movementSpeed = 7; private bool isGrounded = false;
void Update() {
rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0); //Set X and Z velocity to 0
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * movementSpeed, 0, 0);
}
void Jump(){
if (!isGrounded) { return; }
isGrounded = false;
rigidbody.velocity = new Vector3(0, 0, 0);
rigidbody.AddForce(new Vector3(0, 700, 0), ForceMode.Force);
}
void FixedUpdate(){
isGrounded = Physics.Raycast(transform.position, -Vector3.up, 1.0f);
if (isGrounded){
Jump(); //Automatic jumping
}
}
Please write in C# thanks.
Some untested ideas: If your game is 2D and especially if you are using an Orhtographic camera, you can change the Z coordinate based on whether your character is rising or falling. So when rising, he will miss all colliders. Or you could disable colliders when he is rising and reenable them when he is falling. Or you could use a plane mesh for your collider rather than a box collider. Since planes are one-sided, he should pass through going up.
Agreed with robertbu. If there is no reason for you to have to interact with the underside of the object, you can replace the box collider with a plane so that the player can jump 'through' the bottom and land on top. This would be the simplest solution if that is the case you are looking for.
Another simple possibility: turn off the collider on the character when the upward velocity is greater than 0.
Answer by AnomalusUndrdog · Apr 02, 2013 at 08:01 PM
Here's how I would do it. When the player presses jump, take the collider of the player, and change its layer to a new one, maybe name it "PassesThroughTerrain". Configure your collision matrix so that that layer does not collide with the terrain's physics layer. http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html
Then, when the player's Y velocity reaches zero or negative, move it back to its usual layer so it won't pass through anymore when it lands.
Also a better way to check for isGrounded is to have code in your OnCollisionEnter that checks the ContactPoint.normal. See if its Y is positive, if so, that means your player collided with something below it. http://docs.unity3d.com/Documentation/ScriptReference/ContactPoint-normal.html
Thanks a lot. I was finally being able to create jumpThrough platforms with this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095536im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to move while jumping? 1 Answer
OnCollisionEnter2D and OnCollisionExit2D 1 Answer
Struggeling with inverselerp and gradients and adding collider to code generated mesh. 0 Answers