- Home /
Platformer crates(boxes) collision bug
Hi, I have problem with collision my character and crates, problem is when I jump on first crate I can do double jump, but when I touch the crate above the first crate I can do double jump again. I tried OnTriggerStay2D but it's working same as OnTriggerEnter2D, the crated doesn't have Rigidbody2D just BoxCollider2D, so I need to know how to jump just when I touch the top of the crates. You can see my level on image.
//Player
public static int health;
public float speed;
public float climbSpeed;
public float jumpForce;
public float doubleJumpForce;
public bool doubleJump;
private float climbVelocity;
public Rigidbody2D rb2;
private bool isClimbing;
//Teleport
public Transform Teleport1;
private bool teleported1;
public Transform Teleport2;
private bool teleported2;
//UI
public Text healthTxt;
//Other
private float gravityScaleSave;
//Ladders
private bool ladder2Touch;
//Ground
public bool grounded;
public BoxCollider2D floor1;
public BoxCollider2D floor2;
//Box
public bool boxGround;
//Keys
private bool redKey;
private bool blueKey;
// Use this for initialization
void Start () {
healthTxt = GameObject.Find ("HealthShow").GetComponent<Text> ();
health = 100;
blueKey = false;
gravityScaleSave = rb2.gravityScale;
rb2 = GetComponent<Rigidbody2D> ();
rb2.freezeRotation = true;
}
// Update is called once per frame
void Update () {
healthTxt.text = "Health: " + health;
//move
var move = new Vector3 (Input.GetAxis ("Horizontal"), 0, 0);
transform.position += speed * move * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.W) && grounded == true && boxGround == false) {
rb2.AddForce(Vector2.up * jumpForce,ForceMode2D.Impulse);
doubleJump = true;
}
if (Input.GetKeyDown (KeyCode.W) && grounded == false && boxGround == false) {
if(doubleJump == true){
rb2.AddForce(Vector2.up * doubleJumpForce,ForceMode2D.Impulse);
doubleJump = false;
}
}
if (Input.GetKeyDown (KeyCode.W) && boxGround == true && grounded == false) {
rb2.AddForce(Vector2.up * jumpForce,ForceMode2D.Impulse);
doubleJump = true;
}
if (Input.GetKeyDown (KeyCode.W) && boxGround == false && grounded == false) {
if(doubleJump == true){
rb2.AddForce(Vector2.up * doubleJumpForce,ForceMode2D.Impulse);
doubleJump = false;
}
}
//climbing
if (isClimbing == true) {
if(ladder2Touch == true) {
floor2.isTrigger = false;
} else {
floor2.isTrigger = true;
}
floor1.isTrigger = true;
rb2.gravityScale = 0;
climbVelocity = climbSpeed * Input.GetAxisRaw ("Vertical");
rb2.velocity = new Vector2 (rb2.velocity.x, climbVelocity);
} else {
floor1.isTrigger = false;
floor2.isTrigger = false;
rb2.gravityScale = gravityScaleSave;
}
}
public void OnTriggerEnter2D(Collider2D coll) {
if (coll.gameObject.name == "Ladder1") {
isClimbing = true;
}
if (coll.gameObject.name == "Ladder2") {
isClimbing = true;
ladder2Touch = true;
floor2.isTrigger = false;
}
if (coll.gameObject.name == "blueKey") {
blueKey = true;
Destroy(GameObject.Find("blueKey"));
}
if (coll.gameObject.name == "redKey") {
redKey = true;
Destroy(GameObject.Find("redKey"));
}
if (coll.gameObject.name == "spikes") {
health -= 10;
}
if (coll.gameObject.name == "Teleport1") {
StartCoroutine(Tel1(1F));
if (teleported1 == false){
transform.position = Teleport2.transform.position;
teleported2 = true;
}
}
if (coll.gameObject.name == "Teleport2") {
StartCoroutine(Tel1(1F));
if (teleported2 == false){
transform.position = Teleport1.transform.position;
teleported1 = true;
}
}
}
public void OnTriggerExit2D(Collider2D coll) {
if (coll.gameObject.name == "Ladder1") {
isClimbing = false;
}
if (coll.gameObject.name == "Ladder2") {
isClimbing = false;
ladder2Touch = false;
floor2.isTrigger = false;
}
if (coll.gameObject.name == "Teleport1") {
teleported1 = false;
}
if (coll.gameObject.name == "Teleport2") {
teleported2 = false;
}
}
public void OnCollisionStay2D(Collision2D coll) {
if (coll.gameObject.tag== "Ground") {
grounded = true;
floor2.isTrigger = false;
boxGround = false;
}
if (coll.gameObject.tag == "Box") {
boxGround = true;
grounded = false;
}
}
public void OnCollisionEnter2D (Collision2D coll) {
if (coll.gameObject.tag== "Ground") {
grounded = true;
}
if(coll.gameObject.name == "blueGate") {
if (blueKey == true) {
Destroy(GameObject.Find("blueGate"));
}
}
if(coll.gameObject.name == "redGate") {
if (redKey == true) {
Destroy(GameObject.Find("redGate"));
}
}
}
public void OnCollisionExit2D (Collision2D coll) {
if (coll.gameObject.tag == "Ground") {
grounded = false;
boxGround = false;
}
if (coll.gameObject.tag == "Box") {
boxGround = false;
}
}
IEnumerator Tel1(float time) {
yield return new WaitForSeconds(time);
teleported1 = false;
}
IEnumerator Tel2(float time) {
yield return new WaitForSeconds(time);
teleported2 = false;
}
hard to tell from here; but have you checked all layers and physics 2d settings for collisions ? Just to start with that.
Yes, I checked. But I just need to jump from the top of the crate, no jump from side.
sure, you can make it not listed so you keep private. then it will be easier to understand
ok so basically, you want to be able to double jump 'only' when you are a the top of a crate, is it that ?
Ok now i got it. So basically, there's no way to make a BoxCollider2D check for faces (up, right, left, etc) you need to make 2 colliders, one for bottom and one for top; i show you in example. One is child of other.
Alternatively, if it's the 'stuckiness' you can set the $$anonymous$$aterial to be like 'ice'. Simply create a 2D Physics 2D $$anonymous$$aterial (in the Project Folder) then put Friction to 0
Let me know how it goes.
Answer by DiscoFever · Nov 26, 2015 at 03:48 PM
I would then start over, simple step by step. Your code is very messy and confusing, it's hard to understand. You definitely use RayCasting for that kind of things. There's some force that is being triggered but in the wrong place; it's hard to find unless debugging.
If i were you i would get an asset that is made for this kind of things and work from there; a good one is this one, worth the price : https://www.assetstore.unity3d.com/en/#!/content/47229
Sorry that I couldn't help more. I wish I would, but maybe someone will.
Good luck !