Tuesday, March 31, 2015

Unity 2D One Way Platform

Lately I've been using a lot of Unity's new 2D features, and one thing that has been driving me nuts is the Platform Effector 2D, specifically it's One Way function.

 The One Way Effector doesn't ignore horizontal collisions, and only ignores vertical collisions while the colliding object is travelling upwards. When an object's upward momentum is not great enough to clear the platform the Effector kicks in and violently pushes the object away, or, if the object has multiple colliders (like the Standard Assets Platformer Character) it may get stuck half-way through. This may be the very literal description of a one-way collider, but in practice it's not very useful.

I made numerous attempts to create a better One Way Platform and in the end I settled on the simplest solution. My method utilizes a Trigger Collider on the character(s), and a Tag on the One Way Platforms.

Note: Rather than using tags, an alternative method would instead assign One Way Platforms to a specific layer.  The code would then compare the layer instead of the tag.  The benefit to this would be to easily allow certain objects (like projectiles or flying characters) to completely ignore One Way Platforms via Physics2DSettings > LayerCollisionMatrix.



using UnityEngine;
using System.Collections.Generic;

[RequireComponent (typeof (Collider2D))]

public class OneWayController : MonoBehaviour {

public String m_OneWayTag = "OneWayPlatform"; //Tag to use for one way platforms

List <Collider2D> m_ActorColliders; //List of colliders on this character

void Awake ()
{
//Cache refrences to this character's colliders in a list
m_ActorColliders = new List<Collider2D>();

Collider2D[] colliders = GetComponents<Collider2D>();

for (int i = 0; i < colliders.Length; i++) {

//Exclude the one way trigger from the collider list
if (!colliders[i].isTrigger)
m_ActorColliders.Add(colliders[i]);
}
}

void OnTriggerEnter2D (Collider2D other)
{
//When a platform with the one way tag enters the trigger
                //ignore collisions between it and this character
if (other.Tag == m_OneWayTag) {

for (int i = 0; i < m_ActorColliders.Count; i++) {

Physics2D.IgnoreCollision(other, m_ActorColliders[i], true);
}
}
}

void OnTriggerExit2D (Collider2D other)
{
//When the platform exits the trigger re-enable collisions
if (other.Tag == m_OneWayTag) {

for (int i = 0; i < m_ActorColliders.Count; i++) {

Physics2D.IgnoreCollision(other, m_ActorColliders[i], false);
}
}
}

The result is One Way Platforms that allow characters to pass through them on three sides without interruption.  The only drawback is that raycasts could be blocked by the trigger colliders on characters, if that is an issue you can go to Edit > Project Settings > Physics 2D > and uncheck "Raycasts Hit Triggers".

62 comments:

  1. if I had the money I would hire Off Site SEO out. If I didn't have the money (which I don’t) I would focus on building relevant links to my site. I would rotate between 10 or so different anchor texts but I would be heavy on my main 1 or 2 terms. These are the different types of links I would focus on getting. 1 Ways links, Recips, 3 Ways, Article, Directory (free and a select few paid), Forum Sigs, Offsite Blogs, Blog Comments, and Press Releases. Webdesign

    ReplyDelete
  2. It is true that 44% of small businesses do not have a website or blog. They are unable to reach 73% of Internet users each day. If your company has a website, customers can research the latest products the business owner's company offers. Webdesign Limburg

    ReplyDelete
  3. If you do not have a logo or anything by which your customers currently know you, do you have any ideas about what you might like, a sketch, a doodle or something similar? Whatever the case, usually a professional web design company can design a professional logo for you or reuse your current one. Webdesignbetaalbaar.be

    ReplyDelete
  4. It calls for countless hours in front of a computer, learning, reading tutorials, and trial and error until you get it down. But learning is only the beginning now you have to make a portfolio, and look for work. The competition on free lance job boards is fierce. Webdesignprofessioneel.be

    ReplyDelete
  5. Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! top software tools for writers

    ReplyDelete
  6. The writer has outdone himself this time. It is not at all enough; the website is also utmost perfect. I will never forget to visit your site again and again. https://fmovies.group

    ReplyDelete
  7. You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! https://couchtuner-hd.com

    ReplyDelete
  8. I found this is an informative and interesting post so i think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this article. https://putlocker-live.org

    ReplyDelete
  9. Keep up the good work; I read few posts on this website, including I consider that your blog is fascinating and has sets of the fantastic piece of information. Thanks for your valuable efforts. https://solarmovies.video

    ReplyDelete
  10. Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. https://soap2day.digital

    ReplyDelete
  11. This was a really great contest and hopefully I can attend the next one. It was alot of fun and I really enjoyed myself.. https://megashare-website.com

    ReplyDelete
  12. Very nice article, I enjoyed reading your post, very nice share, I want to twit this to my followers. Thanks!. https://solarmovies.cloud

    ReplyDelete
  13. Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! https://rainierland.live

    ReplyDelete
  14. Admiring the time and effort you put into your blog and detailed information you offer!.. https://primewire.today

    ReplyDelete
  15. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free https://xmovies8.one

    ReplyDelete
  16. Nice post. I was checking constantly this blog and I’m impressed! Extremely useful info specially the last part I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck.  https://vexmovies.pw

    ReplyDelete
  17. Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!! https://losmoviess.com

    ReplyDelete
  18. I am glad you take pride in what you write. This makes you stand way out from many other writers that push poorly written content. https://putlocker.quest

    ReplyDelete
  19. I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... https://putlocker.work

    ReplyDelete
  20. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. https://projectfreetv.space/

    ReplyDelete
  21. I think that thanks for the valuabe information and insights you have so provided here. https://y-ymovies.com

    ReplyDelete
  22. Hi there! Nice material, do keep me posted when you post something like this again! I will visit this blog leaps and bounds for more quality posts like it. Thanks... https://vumoo.vip

    ReplyDelete
  23. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. https://solar-movies.one

    ReplyDelete
  24. Attractive, post. I just stumbled upon your weblog and wanted to say that I have liked browsing your blog posts. After all, I will surely subscribe to your feed, and I hope you will write again soon! https://currentputlocker.site

    ReplyDelete
  25. Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon https://thecouchtuner.space

    ReplyDelete
  26. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. https://soap2daymovies.website

    ReplyDelete
  27. Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again. https://123moviesnow.space

    ReplyDelete
  28. Wonderful blog! Do you have any tips and hints for aspiring writers? Because I’m going to start my website soon, but I’m a little lost on everything. Many thanks! https://1solarmovies.site

    ReplyDelete
  29. You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! https://putlocker-movies.space

    ReplyDelete
  30. Without fail, your writing style is top professional; even your website also looks amazing thank you for posting. https://la-123movies.one

    ReplyDelete
  31. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. https://123putlocker.website

    ReplyDelete
  32. I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious https://soap2day.shop

    ReplyDelete
  33. Remarkable article, it is particularly useful! I quietly began in this, and I'm becoming more acquainted with it better! Delights, keep doing more and extra impressive https://soap2day.monster

    ReplyDelete
  34. Keep up the good work , I read few posts on this web site and I conceive that your blog is very interesting and has sets of fantastic information. https://putlockertv.one

    ReplyDelete
  35. The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface. https://solarmovies.space

    ReplyDelete
  36. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. https://soaptoday.website

    ReplyDelete
  37. Remarkable article, it is particularly useful! I quietly began in this, and I'm becoming more acquainted with it better! Delights, keep doing more and extra impressive https://soaptoday.space

    ReplyDelete
  38. Fabulous post, you have denoted out some fantastic points, I likewise think this s a very wonderful website. I will visit again for more quality contents and also, recommend this site to all. Thanks. https://movies123.win

    ReplyDelete
  39. Keep up the good work; I read few posts on this website, including I consider that your blog is fascinating and has sets of the fantastic piece of information. Thanks for your valuable efforts. https://go123movies.site

    ReplyDelete
  40. Remarkable article, it is particularly useful! I quietly began in this, and I'm becoming more acquainted with it better! Delights, keep doing more and extra impressive! https://newputlocker.website

    ReplyDelete
  41. This article was written by a real thinking writer without a doubt. I agree many of the with the solid points made by the writer. I’ll be back day in and day for further new updates. https://thesolarmovies.space

    ReplyDelete
  42. Remarkable article, it is particularly useful! I quietly began in this, and I'm becoming more acquainted with it better! Delights, keep doing more and extra impressive https://watchcouchtuner.space

    ReplyDelete
  43. i love reading this article so beautiful!!great job! indian visa application

    ReplyDelete
  44. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. 토토사이트

    ReplyDelete
  45. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. Canada Visa Online

    ReplyDelete
  46. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. 토토커뮤니티

    ReplyDelete
  47. It is somewhat fantastic, and yet check out the advice at this treat. UNITED STATES VISA FOR SPANISH CITIZENS

    ReplyDelete
  48. hi was just seeing if you minded a comment. i like your website and the thme you picked is super. I will be back. INDISK VISUM TIL FORRETNINGSMØDE

    ReplyDelete
  49. There you will then see the most important thing, the application provides you a website a powerful important internet page: VISA PERNIAGAAN INDIA

    ReplyDelete
  50. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Domanda di visto per il Canada

    ReplyDelete
  51. Thank you for some other informative website. The place else may just I get that kind of information written in such a perfect method? I have a venture that I am simply now running on, and I’ve been at the glance out for such info. http://www.cantorburger.com/2021/02/06/make-money-easy-with-this-online-cards-game/

    ReplyDelete
  52. What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. BUSINESS VISA FOR INDIA

    ReplyDelete
  53. Your texts on this subject are correct, see how I wrote this site is very good. fusible interlining fabric

    ReplyDelete
  54. There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about interestingly. good iptv

    ReplyDelete
  55. Gives you the best website address I know there alone you'll find how easy it is. link pos4d

    ReplyDelete
  56. I love the way you write and share your niche! Very interesting and different! Keep it coming! Roblox Shirt Template

    ReplyDelete
  57. Birthday Decoration Order Birthday Balloon Decoration Online in City . Get Upto 50% Off on All Packages For Birthday Anniversary Baby shower Romantic Room Decoration. Birthday Decoration 100% Customer Satisfaction Guranteed..

    ReplyDelete
  58. Digital Kumar is based in Delhi/NCR, A Top-rated partner with Amazon Service Provider Network (SPN) and Flipkart that enables sellers to sell their products online. We have overseen accounts in 10+ categories and generated crores of revenue. We provide the below services: Amazon listing services, Amazon product listing services, Amazon account management, Amazon account management services, and Amazon seller account suspended.

    ReplyDelete
  59. I came onto your blog while focusing just slightly submits. Nice strategy for next, I will be bookmarking at once seize your complete rises... red boost supplement

    ReplyDelete
  60. Kheloyar stands as India's most reliable online gaming platform. Enjoy a swift 50% welcome bonus alo

    ReplyDelete