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
1
Question by nerf_herder42 · Apr 05, 2020 at 12:59 AM · unity 2drigidbody2dcollider2d

Can I create boundaries around my level without using box colliders?

I have a level in which the player (kinematic - rigidbody2d, box collider) moves around the screen, avoiding enemies and whatnot. I want to have a boundary around the level so the player doesn't disappear through the sides of the scene, I'd also like to have obstacles in the scene just to add difficulty.

I'm having problems with this because my player is kinematic, and the player floats through colliders. The wall is a static rigidbody2d with a box collider Is there any way I can create a boundary in order to 'fake' the walls? Or is there some way I can force a kinematic gameobject to collide?

Thanks in advance guys!

Scene player inspector

scene.png (51.8 kB)
inspector-p.png (63.9 kB)
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

3 Replies

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

Answer by BBIT-SOLUTIONS · Apr 06, 2020 at 01:09 AM

I have 2 possible ideas, not sure which one you should prefer or which works "better":

  1. Use the box collider as isTrigger. And then, when the player is inside the trigger just avoid moving your player.

  2. It looks like your Gamefield is always rectangle. So you actually know the borders and can use the coordinates of it, because of that a movememt script of this kind should work:

     float xMin = -5f;
     float xMax = 5f;
     float yMin = -7f;
     float yMax = 10f;
         
     void Move(){
         Vector3 tmp = transform.position;
         if(tmp.x < xMin){
             tmp.x = xMin;
         }
         else if(tmp.x > xMax){
             tmp.x = xMax;
         }
         else if(tmp.y < yMin){
             tmp.y = yMin;
         }
         else if(tmp.y > yMax){
             tmp.y = yMax;
         }
         transform.position  = tmp;
     }
     
    
    

The values are just placeholders. Set your correct values for the min and max variables to the positions of your borders.

Comment
Add comment · Show 24 · 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 nerf_herder42 · Apr 06, 2020 at 01:21 AM 0
Share

Thank you so much!! Your second suggestion worked perfectly for my outer walls. I tried the first suggestion for obstacles in the scene but found my player got stuck when I set the speed to 0, assu$$anonymous$$g because I have collision detection set to continuous, don't suppose you havee any other excellent suggestions for this? XD

avatar image BBIT-SOLUTIONS nerf_herder42 · Apr 06, 2020 at 01:32 AM 0
Share

Oh you are right. Did not thought about the inner obstacles.

$$anonymous$$aybe a similar mix between the both approaches could work. Just a little Pseudo-code to describe the idea (you should assign that script then to each inner obstacle and mark their collider as trigger):

 float my$$anonymous$$inX, my$$anonymous$$axX, my$$anonymous$$inY, my$$anonymous$$axY;
 
 void Awake(){
     my$$anonymous$$inX = transform.position - 0.5f; //use the (half) width of your element ins$$anonymous$$d of 0.5
     my$$anonymous$$axX = transform.position + 0.5f;//same here
     /* same for Y and height of obstacle...   */
 }
 
 void OnTriggerEnter2D(Collider2D other){
 
     if(other == player){
         //use here the same approach like for the outer border
         tmp = other.transform.position;
         if(tmp.x > my$$anonymous$$inX){
             tmp.x = my$$anonymous$$inX;
         }

         /*...same for the other for borders*/

         other.transform.position = tmp;
     }
 
 }

 
avatar image BBIT-SOLUTIONS BBIT-SOLUTIONS · Apr 06, 2020 at 01:47 AM 0
Share

Or maybe ins$$anonymous$$d of using OnTriggerEnter2D() in this case also OnTriggerStay2D() could be more useful, not sure...

Show more comments
avatar image
0

Answer by warthos3399 · Apr 05, 2020 at 02:23 AM

Create a empty object, assign it a box collider, adjust, and box in the outer walls/perimeter.

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 nerf_herder42 · Apr 06, 2020 at 12:47 AM 0
Share

Thanks for the reply, I can't use colliders as my player object must be kinematic, which doesn't allow for typical collisions. Is there a way i can use the OnTriggerEnter2D() method to fake collision?

avatar image
0

Answer by Vipertex13 · Apr 05, 2020 at 05:15 AM

@nerf_herder42 sorry this way is better //Keeps the player inbounds public float xRange if (transform.position.x < -xRange) { transform.position = new Vector3(-xRange, transform.position.y, transform.position.z); } if (transform.position.x > xRange) { transform.position = new Vector3(xRange, transform.position.y, transform.position.z); }

Comment
Add comment · Show 5 · 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 nerf_herder42 · Apr 06, 2020 at 12:49 AM 0
Share

Thanks for the reply, I tried your suggestion, but I'm having the same issue with the kinematic player just floating through the walls. Is there something different I can do with a mesh collider to stop this?

avatar image Vipertex13 nerf_herder42 · Apr 06, 2020 at 01:52 AM 0
Share

Here i edited the post check it again $$anonymous$$ost simple and easy code

avatar image Vipertex13 nerf_herder42 · Apr 06, 2020 at 01:55 AM 0
Share

This only xAxis you can also make it yAxis too

avatar image Vipertex13 · Apr 06, 2020 at 01:59 AM 0
Share

This will work better so that you can just pinpoint the boundary location on X and Y Axis

avatar image miracalious · Jun 10, 2020 at 08:40 PM 0
Share

Can be done more simply by clamping.

transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, 0, x$$anonymous$$ax), $$anonymous$$athf.Clamp(transform.position.y, 0, y$$anonymous$$ax), transform.position.z);

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

138 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

Related Questions

2018.2.17 OnTriggerEnter2D not working 1 Answer

Why do GameObjects need Rigidbodies to collide? 0 Answers

How is Rigidbody 2D Auto Mass calculated? 2 Answers

Moving colliders that are part of a composite collider 1 Answer

Making 2D objects solid, so that they do not pass through eachother? 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