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
1
Question by Alienjesus · Mar 01, 2011 at 10:35 AM · movementraycastcolliderparenting

Making object found with raycast a child of the object the ray is cast from

OK, another question from me, hopefully you aren't all getting sick of them yet.

So I have an object which moves on 2d axes in relation to a marker tracked by a camera. I want to be able to use this object (called 'controller' from here on) to manipulate other objects in the background. To do this I'm using a raycast (triggers proved problematic) to detect other objects behind the one the controller. My final plan is to make it so that if an object is being found by the raycast, then on a button press that object will become a child of the controller, become unable to effect the environment (until its replaced), hopefully become transparent and then be able to be replaced elsewhere with another button press. I'm kinda new to Unity, so i'm taking it in small steps, the first of which is to make the object a child of the controller as soon as the ray finds it.

However, I don't really know how to write code that triggers the other game object - anything I write corresponds to the controller object that casts the rays in the first place. All I want to know for now is how I write script that effects the object that is being detected by the raycaster, rather than the one thats casting it, and has the script attached in the first place? Do I write it in the same script that casts the rays, a new script attached to the other gameobject or both?

Here's my current code which is attached to the controller object:

function Update () { var fwd = transform.TransformDirection (Vector3.forward); //Defining the travel direction of the raycast vector var hit : RaycastHit; //If Raycast collides with something, hit = true? var LayerMask = 1 << 9; //Interacts with 1 layer - Layer 9 - Moveable

//Draws ray vector in Scene view Debug.DrawRay(transform.position, fwd * 50, Color.green);

//if Raycast finds a collider in front of this object in layer 9, print message 1, else print message 2. if (Physics.Raycast (transform.position, fwd, hit, 50, LayerMask)) { print ("There is something in front of the object!");
}

     else print ("Nothing in front of the object!");
     }

P.S if something is wrong with the idea of using a raycast to do all the stuff I mentioned before, feel free to say! I don't mind being told now and saving time in the long run!

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

Answer by Novodantis 1 · Mar 01, 2011 at 11:13 AM

All you need is to retrieve the other object involved in the hit, and parent it. Several parts of this object are properties of RaycastHit (if you select any keyword in the script and hit F1, Unity should show the help file detailing that function or class and its properties).

So in the section where we find something in front of the object, we just need to identify it:

var otherThing = hit.transform; // assigns variable 'otherThing' to the object

Then parent that object to this one

otherThing.parent = transform;
Comment
Add comment · Show 2 · 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 Alienjesus · Mar 01, 2011 at 11:15 AM 0
Share

Thank you very much. Such a simple and elegant solution, I just couldn't seem to find how to do it online. Java is new to me though, so I probably overlooked it plenty ^_^

avatar image Frost · Apr 14, 2011 at 12:14 PM 0
Share

Thank you so much. I had this nagging problem for hours. Total timesaver

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

No one has followed this question yet.

Related Questions

Raycasting on Moving Colliders 0 Answers

Move player to specific location and avoid obstacle. 2 Answers

Moving objects using raycasting? 1 Answer

Raycasts left/right problem. One works, other doesn't...? 1 Answer

Enemy AI passing through walls 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