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 niorg2606 · Aug 06, 2017 at 03:28 PM · 2dspriteslayerstop-down

2D Top-Down 3D-Like effect

Hi, I'm making a 2D Top-Down game using Unity 2D. However, I've ran into a feature I want to add, but don't know how. Here's what I'm trying to do: When the player is in front of an object, they should be on top of the object in the sprite's sorting order., and when the player is behind an object, they should be below the object. How would I achieve this? Thanks alt text

playerinfront.png (1.9 kB)
playerinback.png (2.4 kB)
Comment
Add comment · Show 1
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 Cherno · Aug 06, 2017 at 05:18 PM 0
Share

Search for "sprite depth sorting", there are lots of tutorials and samplescripts for this as it is a common feature of top-down 2d games. One way to do it is to check the character sprite's y value and use this to set it's renderer's sorting order.

2 Replies

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

Answer by Eidern · Aug 06, 2017 at 07:23 PM

Hello, I suggest you to have a look at the Unity 2D best practice showed here:(2014, i know but it fits perfectly for what you're asking) :

https://www.youtube.com/watch?v=HM17mAmLd7k

at 34:00, with a script onto the object which every update will compare the y.pos of it's Anchor and use it to change its z-index on the fly :

     void Update(){
      renderer.sortingOrder = (int)(transform.position.y * -10);
     }

Like this, when you're object is got an y coord lower than the other objects it displays above, and when it's higher y it displays behind. But watch the video, there are some other nice tricks likeplaying with anchors for exceptionnal objects that you'd like to put on top even if they're higher..

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
avatar image
0

Answer by moltow · Aug 06, 2017 at 05:50 PM

As @Cherno suggested, there are many ways to do this, but for the sake of the discussion, let's assume you're just using the Default SortingLayer. Further, let's assume that the bucket's OrderInLayer property is set to 1. You could then have your Character's script do something like:

     SpriteRenderer renderer;
 
     void Start () 
     {
         renderer = GetComponent<SpriteRenderer>();
     }
 
     void MoveSpriteToBack()
     {
         renderer.sortingOrder = 0;
     }
 
     void MoveSpriteToFront()
     {
         renderer.sortingOrder = 2;
     }
 
Comment
Add comment · Show 1 · 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
avatar image niorg2606 · Aug 06, 2017 at 10:09 PM 0
Share

How would I make this work for multiple objects at once?

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

125 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 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 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

What should the best practice be to teleport player on top-down game 1 Answer

Unity sprites and sorting layers messed up 1 Answer

How can I make my Enemy look/flip towards my player (2D TOP-DOWN) 1 Answer

Select 2D Object By Mouse ? 4 Answers

Crop/Mask a Sprite on runtime 0 Answers


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