• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

jump through platform causes double jump 1 Answer

Multiple Sub-colliders 1 Answer

BoxCollider not blocking other object(s) 1 Answer

Player passenger moving when being pushed by two platforms 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges