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 /
  • Help Room /
avatar image
0
Question by Ddaron · Oct 20, 2015 at 09:32 PM · collisionjumpcollider2dplatformerplatform

Collider2D of game object does not work, unless I duplicate the game object.

I am trying to implement one way platforms in my "game", I am doing it like this:

 public class OneWayPlatform : MonoBehaviour {

 public Transform player;
 private Collider2D collider;
 private float halfTheSpriteSize;
 private float halfThePlayerSize;
 public bool isPlayerAbove;

 void Start()
 {
     collider = GetComponent<Collider2D>();
     halfTheSpriteSize = collider.gameObject.GetComponent<SpriteRenderer>().bounds.size.y / 2;
     halfThePlayerSize = player.gameObject.GetComponent<Collider2D>().bounds.size.y / 2;

     if (player == null)
     {
         player = GameObject.FindGameObjectWithTag("Player").transform;
     }

 }
 // Update is called once per frame
 void FixedUpdate () {
         Debug.DrawLine(new Vector3(player.position.x, player.position.y - halfThePlayerSize, 0), 
             new Vector3(collider.gameObject.transform.position.x, collider.gameObject.transform.position.y + halfTheSpriteSize, 0), Color.red);
     this.isPlayerAbove = ((collider.gameObject.transform.position.y + halfTheSpriteSize) - (player.position.y - halfThePlayerSize)) < 0;
     if (isPlayerAbove)
     {
         collider.enabled = true;
     }
     else
     {
         collider.enabled = false;
     }
 }

 }

This solution does not work... UNLESS I copy the platform and move it over, then it works perfectly, sometimes...

Is this way of thinking good at all? Is calculating distance on y axis a good solution to make one way platforms?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ddaron · Oct 21, 2015 at 05:31 AM

I fixed the issue. The key was here:

 this.isPlayerAbove = ((collider.gameObject.transform.position.y + halfTheSpriteSize) - (player.position.y - halfThePlayerSize)) < 0;

Due to incosistent sizes of sprites and colliders in my scene (this is a prototype), even though I thought it should be zero, it was not. To correct this issue I used:

 this.isPlayerAbove = ((collider.gameObject.transform.position.y + halfTheSpriteSize) - (player.position.y - halfThePlayerSize)) <= 0.1f;

The solution might not be optimal, but Im staying with the KISS rule for my prototype - and learning example.

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

35 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

Related Questions

jump through platform causes double jump 1 Answer

2D Platformer: Failure to jump through platform 0 Answers

Player wont jump when platform's layer is changed to a created one! 1 Answer

Unable to stop animation upon collision (2d, animation, rigidbody) 0 Answers

How To Check isGrounded Or Not PLEASE HELP 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