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.
– robertbuAgreed 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.
– HypoXic5665Another simple possibility: turn off the collider on the character when the upward velocity is greater than 0.
– robertbu