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
2
Question by jg2115 · Jul 21, 2013 at 11:12 PM · raycastcubeprimitive

Detect if object is a cube(find the primitive type) with physics.raycast

I am making a 3d puzzle game with picking up and placing cubes. I want to only be able to place a cube on a cube. I am using Physics.Raycast to determine where to place the cube. Is there a function I could use to determine the primitive type of the object hit with the Raycast?

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
1
Best Answer

Answer by Eric5h5 · Jul 21, 2013 at 11:24 PM

"Primitives" in Unity are really just arbitrary meshes, so they have no inherent value saying whether they are a cube, sphere, etc. You can write a script where you indicate the type yourself and attach it to the objects, such as

 var primitiveType : PrimitiveType;

Then use GetComponent on the object hit with the raycast to read the primitiveType variable.

Comment
Add comment · Show 3 · 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 jg2115 · Jul 21, 2013 at 11:50 PM 0
Share

so how would i state the getcomponent? primitiveType = hit.transform.gameObject.GetComponent(PrimitiveType);

or what?

avatar image aldonaletto · Jul 22, 2013 at 02:01 PM 0
Share

No, the idea is to save that single line as a script ("ObjectType.js", for instance) and attach it to every object you must identify, then set its value in the Inspector. When hitting an object, get its script ObjectType and verify whether its type is a cube - for instance:

 var objType: ObjectType = hit.GetComponent(ObjectType);
 if (objType){ // if object has ObjectType script...
   switch(objType.primitiveType){ // read the type assigned
     case PrimitiveType.Cube:
       // it's a cube
       break;
     case PrimitiveType.Sphere:
       // it's a sphere
       break;
   }
 }

Notice that you must add the script ObjectType to every object you want to identify, and manually set the type in the Inspector.

avatar image jg2115 · Jul 22, 2013 at 03:00 PM 0
Share

O$$anonymous$$ thanks!

avatar image
3

Answer by aldonaletto · Jul 21, 2013 at 11:35 PM

You could use the mesh name to identify the primitive type - in this case, it's "Cube Instance":

 ...
 var hit: RaycastHit;
 if (Physics.Raycast(ray, hit)){
   var mesh: Mesh = hit.gameObject.GetComponent(MeshFilter).mesh;
   if (mesh.name == "Cube Instance"){
     // object hit is a primitive cube
   }
 }
 ...

But this isn't a good solution: the primitive mesh name could change in future Unity versions and make your game useless. A better (and faster) alternative would be to tag the cubes with a specific tag - like "Cube", for instance:

 ...
 var hit: RaycastHit;
 if (Physics.Raycast(ray, hit)){
   if (hit.gameObject.CompareTag("Cube")){
     // object hit is tagged "Cube"
   }
 }
 ...

NOTE: Remember to register the tags in the Tag/Layer Manager!

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

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

17 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

Related Questions

Primitive Cube in code! 0 Answers

Primitive Cube Texturing Help 3 Answers

Texture Problem on Cube primitive 1 Answer

How i can create a cube with specific coordenates? 3 Answers

create primitive cube a runtime with specific size 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