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 dju4ic · Jun 10, 2011 at 08:39 PM · raycastcolliderobject

Is Object At Location?

I have been looking for a while for a solution to my current problem and am wondering if raycasting or colliders would be the best approach.

Here is my scenario - I am currently working on a 3d map editor via raycasting and a gridding system. I am using transparent prefabs that ignore raycast to show the currently selected object to create, then clicking in location creates the object.

I now need to check if there is already an object at this location, if there is, I need to turn the transparent material color red (showing that you can not build here).

Currently I have no code to detect if there is an object currently at this location and not exactly sure where to start. I am working all with C# and really, the event that is triggered doesn't matter, just a debug statement even saying there is a collision with an object already here would be fine.

I have experimented with colliders with no luck: instantiate and if collider is triggered perform function not working. So decided to wipe the slate clean on this issue and start with some more professional advice on how to tackle it. Thanks.

edit: Code update

I have been attempting with OnCollisionEnter within a script on the object itself.

CODE:

 public class EditCollider : MonoBehaviour {
 public Color _tmpColor;
 public GameObject _tmpObj;
 public int _isColliding;
 
 void Start () {
 _tmpObj = this.gameObject;
 _tmpColor = this.gameObject.renderer.material.color;
 }
 
     void OnCollisionEnter(Collision collision) {
     _isColliding = 1;
     _tmpObj.renderer.material.color = Color.red;
     Debug.Log("Is Colliding: " + _isColliding);
 }
 
 void OnCollisionExit(Collision collision) {
     _isColliding = 0;
     if(!_tmpObj)
         return;
     // Unset raycast targets material color
     _tmpObj.renderer.material.color = _tmpColor;
     Debug.Log("Is Colliding: " + _isColliding);
 }

}

edit: web build location: http://crystalvortex.ca/Unity/WebPlayer.html

Comment
Add comment · Show 3
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 Chris D · Jun 10, 2011 at 09:33 PM 0
Share

your build is crashing for me. Also, what currently happens with your existing script, anything at all?

avatar image dju4ic · Jun 10, 2011 at 09:39 PM 0
Share

webplayer should work now.. corrupt upload fixed (press y for edit mode, not much there yet).. currently i am not even getting triggers from the collision, maybe because it is being instantiated already inside the collider it doesnt know its hitting anything? no debug logs co$$anonymous$$g back.. Also I am trying with mesh colliders and read must use convex which i am.

avatar image Chris D · Jun 10, 2011 at 09:44 PM 0
Share

as I understand it, mesh collider to mesh collider isn't supported - it might be better to use a simplified collider for placement purposes (as you won't really need the accuracy at that point).

3 Replies

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

Answer by dju4ic · Jun 11, 2011 at 02:32 AM

OK, I have come across a solution, very simple, I had to work around and use a different method, but simple enough, I am using a group of objects with a transparent cube that fills the whole grid space, collider is at .9 scale and constrained all on the rigidbody, then added the basic script:

 public class EditCollider : MonoBehaviour {
 private Color _tmpColor;
 private int _isColliding;
 
 void Start () {
     _tmpColor = this.gameObject.renderer.material.color;
 }
 
 void Update() { 
     CheckCollision();
 }
 
 void OnCollisionEnter(Collision collision) {
     _isColliding = 1;
 }
 
 void OnCollisionExit(Collision collision) {
     _isColliding = 0;
 }
 
 public void CheckCollision() { 
     if (_isColliding == 1){
         this.gameObject.renderer.material.color = new Color(255, 0, 0, .5f);
     }
     else { 
         this.gameObject.renderer.material.color = _tmpColor;
         this.gameObject.renderer.material.color = _tmpColor;
         }
 }

}

I also updated the Web build to show the results.

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
1

Answer by Chris D · Jun 10, 2011 at 08:59 PM

  1. put colliders back on your objects

  2. in your update, check for collisions with existing objects

  3. if collision is detected, change your texture/shader (check out Material) and disallow placement

if your code isn't working, post it here so we can pick at it :D

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 dju4ic · Jun 10, 2011 at 09:10 PM 0
Share

updated original post with code

avatar image
0

Answer by Eric5h5 · Jun 10, 2011 at 09:01 PM

Since you're using a grid system, generally you'd represent the map internally with an array. So you can check if the array location is already occupied.

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 dju4ic · Jun 10, 2011 at 09:11 PM 0
Share

well, its very rudimentary - basically takes location on terrain and averages location to instantiate every 4 units.

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

Use ray cast against objects but avoid colliding against other objects 2 Answers

Raycast/Non-Physics Collider Discrepancy 0 Answers

Extend the Collider When Picking Up Objects 0 Answers

Raycast and Hit Problem 0 Answers

Mouse clicking on an object (Raycast.hit) for another object to connect to it? 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