Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by polda602 · Nov 25, 2015 at 05:36 PM · 2dcollision2d-platformer

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;
 
     }

alt text

level1.png (146.9 kB)
Comment
Add comment · Show 12
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiscoFever · Nov 25, 2015 at 06:40 PM 0
Share

hard to tell from here; but have you checked all layers and physics 2d settings for collisions ? Just to start with that.

avatar image polda602 · Nov 25, 2015 at 07:01 PM 0
Share

Yes, I checked. But I just need to jump from the top of the crate, no jump from side.

avatar image DiscoFever polda602 · Nov 25, 2015 at 07:06 PM 0
Share

I don't get it; can you make a quick video ?

avatar image polda602 · Nov 25, 2015 at 07:20 PM 0
Share

Yes, I can, can be the video posted on Youtube ?

avatar image DiscoFever polda602 · Nov 25, 2015 at 07:22 PM 0
Share

sure, you can make it not listed so you keep private. then it will be easier to understand

avatar image polda602 · Nov 25, 2015 at 07:37 PM 0
Share

https://www.youtube.com/watch?v=BrIkcAOd7PQ here

avatar image DiscoFever polda602 · Nov 25, 2015 at 07:43 PM 0
Share

ok so basically, you want to be able to double jump 'only' when you are a the top of a crate, is it that ?

avatar image polda602 · Nov 25, 2015 at 07:51 PM 0
Share

yes, I don't want to stuck on sides of boxes

avatar image DiscoFever polda602 · Nov 25, 2015 at 07:58 PM 0
Share

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. alt text

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.

capture-decran-2015-11-25-a-205509.png (23.5 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

50 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D how to jump just from the top of the box 0 Answers

Trying to freeze multiple gameObjects at the same time 1 Answer

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers

How to check Hypothetical Collisions in 2D 1 Answer

Unity2D side collision detection 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges