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 Matt1000 · Apr 29, 2017 at 02:33 PM · c#2dspritescollider2d2d platformer

Checking for an object at a certain position

Hey, Im trying to make a procedural generation system with a pretty simple grid/tile pattern (2D). In this grid there can be different sprites but due to the fact that square tiled colliders often stop the player when moving I need the game to generate colliders by itself. This is ok, but to do that i need to be able to check where sprites start and end.

All methods for checking i know are using Physics, but since there are no colliders yet i cant do that.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dreamora · Apr 29, 2017 at 07:42 PM

You can ask RectTransforms for the 2D objects actual world extension using RectTransform.GetWorldCorners

RectTransform also provides method for GetLocalCorners as well as the Rect property if you need to know it in local coordinates.

If you need complex colliders at this point, you could dive deeper into what Sprite offers you or alternatively use prefabs that are already fully setup and then use those prefabs in your procedural generator instead of trying to also create the 'blocks' in your procedural general procedurally themself.

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 Matt1000 · Apr 30, 2017 at 08:20 PM 0
Share

how can i use the GetWorldCorners method? I can access the RectTrensform class but not the method... to do this should i be using a sprite's textureRect?

avatar image
0

Answer by tMahon · Apr 29, 2017 at 10:12 PM

You can have the procedural creator make different instances of prefabs that have colliders on them already, you could just use an array of sprites and swap them out at random on an existing grid and do subsequent functions to edit components on a switch(case) for if the tile is ground, wall, item, enemy, etc. If the square colliders being included is causing player movement issues, consider using the colliders as Triggers. Or, depending on how precise your collision needs to be, you could just draw a Physics2D.CircleCastAll from the player or each tile. There's a number of different ways to tackle this, and it's going to depend on more of what you need for the project. There's a really good tutorial on here for a simple way to do procedural top-down 2D I'd recommend for a good starting point on the basics: https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial

Comment
Add comment · Show 2 · 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 Matt1000 · Apr 30, 2017 at 08:16 PM 0
Share

the problem with using prefabs with coliders is that the floor will be made of many square colliders... and this, due to some weird unity bugs, sometimes stops the player from moving.

avatar image tMahon Matt1000 · May 01, 2017 at 03:47 AM 0
Share

Have you considered checking from the player's eye in real time? Do a raycast circle from the player and ask each object what its tag is. Here's an example looking for a wall tag.

Also note: When you instantiate the objects set their tags at the same time, and their colliders to Trigger and this should work. It eli$$anonymous$$ates your standard collision and replaces it manually, I'd recommend using a player controller script that moves transform.position manually and avoid a Rigidbody component altogether to cut down on Debugging code that could bog down movement as you scale.

 // Use this for initialization
     void Start () {
         wallCollidingWith = null;
     }
 
     bool isCollidingWithWall;
     Collider2D wallCollidingWith;
     float xAxis$$anonymous$$ovement;
 
     private void Update() {
         if (!isCollidingWithWall)
         {
             //$$anonymous$$ove normally
         }
         else
         {
             //Detect wall position and freeze that axis
             if (wallCollidingWith.transform.position.y == transform.position.y)
             {
                 xAxis$$anonymous$$ovement = 0.0f;
             }
         }
     }
     //Call physics stuff in FixedUpdate 
     void FixedUpdate () {
         //Cast a circle of 0.5 radius from player location and store all colliders
         Collider2D[] colliders = Physics2D.OverlapCircleAll(point: transform.position, radius: 0.5f);
                 //Set the wall colliders to 'Trigger' to avoid unwanted collision with player collider
         
         for (int i = 0; i < colliders.Length; i++)
         {
             //Check in the array for wall tags. $$anonymous$$ake sure to add something if you're intersecting 2 walls, this will ruin corners as is but you get the point
             if (colliders[i].gameObject.CompareTag("Wall"))
             {
                 wallCollidingWith = colliders[i];
             }
         }
     }

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

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

Dialouge System [SOLVED] 1 Answer

The player randomly freezes in place while other objects move ingame 1 Answer

Bullet hit the collider of a child object 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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