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 /
  • Help Room /
avatar image
0
Question by Wohran · Feb 15, 2017 at 09:13 AM · raycastparentlayermask

transform.SetParent() not setting the object I want as parent.

Hi everyone!

I'm having a problem with transform.SetParent().

Basically, I want my character to have a precise object of the car he is in as a parent. My car has multiple child objects, themselves having children too. The specific object I want as a parent is the child of another child of the car (I hope this is clear enough). I'm trying to cast a ray from the character towards the ground to detect what is just under him and set what it hits as parent.

But despite my use of raycast and layermask, it is not working. My car has its layer set as "Ignore Raycast", and the only object I want as a parent has the layer I added. But whenever I call my raycast to detect the object I'll set as a parent, the hit infos give me the car object, despite its layer.

Does anyone have an idea to fix this? Thanks a lot!

Comment
Add comment · Show 2
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 hexagonius · Feb 16, 2017 at 11:36 PM 1
Share

check this answer, which is probably the same reason (compound collider)

http://answers.unity3d.com/questions/393713/how-to-raycast-a-child-collider-of-a-parent-rigidb.html

avatar image Wohran hexagonius · Feb 17, 2017 at 08:04 AM 0
Share

Perfect, that was exactly my problem and this solution works! Thank you very much!

2 Replies

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

Answer by Wohran · Feb 17, 2017 at 08:06 AM

My character can be inside and outside the car, hence the fact that I want him to be a precise child of the car when he is Inside it.

But the solution hexagonius linked worked perfectly: using hit.collider.transform instead of hit.transform in order to not get the parent of the object you hit but the object itself.

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
0

Answer by CarterG81 · Feb 16, 2017 at 11:47 PM

Why would you want to do some complex raycasting to find a child of the car object, in the first place?

Why not simply have this handled in your Car's script, with a reference to its child object that the player needs to be parented to? Are these components moving constantly & switching what's underneath? If they're static, just store a reference to them somehow.

 //Car.cs
 public GameObject PreciseObjectToParentTo; //Assign this in the Inspector
 //In Code
 playerObject.SetParent(PreciseObjectToParentTo);

Something more manageable, like that?

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

117 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

Related Questions

Use Raycast to select empty parent game object? 1 Answer

RayCastHit 2d with layerMask not workin 0 Answers

See RayCast with layermask 0 Answers

Raycast hitting layer explicitly told to ignore (and doesn't even have collider!) 1 Answer

How do Contact Filters work? 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