Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GoodGuyA · Oct 24, 2014 at 06:40 PM · collisioncollider2dtiling

Altering Collision Based on Proximity

I am currently creating a randomly generated road in Unity. The road itself works exactly as intended, but now the collision becomes the tricky part.

My road is made of square tiles, and I want the player to walk on the interior. What I have done is I set up for separate Box Collider 2D to the prefab which create the cubes in this road.

What I want to do is have each of the tiles check for if there's a tile to any given side of, and if there is, then disable the respective Box Collider 2D based on that. If there is another tile to the direct left and to the direct right of one tile, it will disable its Box Colliders that lie on the left and right sides. If one detects a tile above and to the right of itself, it will disable the top Box Collider 2d as well as the right, and so on.

Th two major questions here are:

  1. How can I do a check of if another object is within proximity (in this case basically touching it) within the script?

  2. How can I individually turn off Box Collider components on a prefab, rather than the whole lot of them?

Thanks a bunch!

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 Anxo · Oct 24, 2014 at 06:46 PM

  1. Look at the docs for Raycast and Raycasthit to draw a ray somewhere to see if it hits a collider, that will show you if there is something in the proximity, you could also check it with sphearCast.

  2. To turn off a component of an object you can collect it in a varible first or just find and turn it off like so.

    player.gameObject.GetComponent().enabled = false;

Comment
Add comment · Show 4 · 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 GoodGuyA · Oct 24, 2014 at 06:55 PM 0
Share

Had a feeling it would have to be raycasting. Will check on that.

Is there a way to distinguish between components though? That script won't discern between which component it will disable. I need to be able to specifically target the cardinal directions to enable/disable them accordingly. They all have different values, of course, but how can I differentiate them in code?

avatar image Anxo · Oct 24, 2014 at 07:24 PM 0
Share

what do you mean? the GetComponent is specific. Unless you mean that you have multiple copies of the same type on one game object, which I can not find the sense in but should it be the case that you are using instances of classes like variables then you could add a variable to the component that is unique to each component and check it.

 SomeComponent wantedComponent = null;
 SomeComponent componentsOfTypeOnObject =      gameObject.GetComponents<SomeComponent>();
 foreach(SomeComponent sc in componentsOfTypeOnObject){
   if(sc.assignedID == "notGoodPratice")
    {
     wantedComponent = sc;
    }
 }

WUCC

But really, I would not have multiple instances of a single type component on one game Object. There are times of-cause where it is needed but I am doubtful that this is the case. How are you using it, why do you need to ad multiple copies of a script to a gameobject.

avatar image GoodGuyA · Oct 24, 2014 at 07:32 PM 0
Share

What would you reccommend other than having the four box colliders? At any given time the box (in this case) will be blocking two sides of the path whilst allowing the other two to go by. That's why I needed the Raycast solution, to detect which sides should be opened up.

I wanted to try and do this in a reusable as possible way, and especially for the corner pieces, not have to make a separate object to be spawned at the four ends of the square.

avatar image Anxo · Oct 24, 2014 at 07:42 PM 0
Share

I usually make child objects who each have their own collider, specially if I need to differentiate between them at collision.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how do I enable collision? 0 Answers

Tilemap Collider 2D preventing objects from moving 2 Answers

How do you trigger a collider to become enabled after the player collider isn't touching it anymore? 4 Answers

2DColliders going through eachother 1 Answer

How do I get onmouseenter to trigger even if another collider is blocking th? 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