• 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 dusty101011 · Mar 22, 2014 at 10:32 PM · raycasttransformparentchild

Having trouble with raycast rigidbody object childing for tractor beam

I'm working on my first game, so forgive any noobish mistakes.

So I have this spacecraft and I've been working on this 'tractor beam' of sorts where I can use a laser Raycast to detect an object in a certain distance in front and then drag that physics object with my spacecraft around and be able to also detach it when mouse if released.

Here's my code so far.

bool laser = Input.GetMouseButton(0);

RaycastHit hit;

Debug.DrawRay(transform.position,transform.forward * 50);

     if (laser == true) {
         if(Physics.Raycast(transform.position,transform.forward, out hit,50)) {
             rayhit.text = "LOCKED ON";
             hit.collider.transform.parent = transform.parent;


         }
         else {
             rayhit.text = "Ready";
         

         }


     }


The problem being the target won't mimic my spacecraft's movements once it has "locked on". I've been trying to make it a child of my spacecraft, but that doesn't seem to work.

Note: Not sure if this effects it, but both objects are rigidbodies and the spacecraft moves around using AddForce with keyboards (like thrusters).

Comment
Add comment · Show 4
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 getyour411 · Mar 22, 2014 at 10:33 PM 0
Share

Your parent/child approach sounds ok to me, perhaps the issue is that once locked on, you need to stop all forces affecting the target object

rigidbody.velocity = Vector3.zero

avatar image dusty101011 · Mar 24, 2014 at 12:23 AM 0
Share

That didn't really work. I managed to get some movement using addRelativeForce, but that ends up shooting the object if it loses the tractor beam.

avatar image dusty101011 · Mar 24, 2014 at 10:18 PM 0
Share

Thanks Lo0Nuhti$$anonymous$$. Works perfectly!

avatar image Lo0NuhtiK · Mar 24, 2014 at 10:26 PM 0
Share

Yw.

1 Reply

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

Answer by Lo0NuhtiK · Mar 24, 2014 at 01:32 AM

Change the caught objects rigidbody to isKinematic while it's caught and try that. Something like this :

 RaycastHit hit ;
 
 Transform caughtObj ;
 Rigidbody caughtRig ;
 
 Transform myTrans ;
 Rigidbody myRigid ;
 
 void Start()
 {
    myTrans = this.transform ;
    myRigid = this.rigidbody ;
 }
 
 void Update()
 {
    GrabStuff() ;
 }
 
 void GrabStuff()
 {
    if(Input.GetMouseButtonUp(0)) //if we let go of this thing, drop it
    {
       //rayhit.text = "Ready" ;
 
       if(caughtObj)
       {
          caughtObj.parent = null ;
          if(caughtRig)
          {
             caughtRig.isKinematic = false ;
             caughtRig = null ;
          }
 
          caughtObj = null ;
       }
    }
 
    if(caughtObj) //early-out if already caught something
       return ;
 
    if(Input.GetMouseButton(0)) //if we're trying to grab something
    {
       Debug.DrawRay(myTrans.position, myTrans.forward * 50f) ;
 
       if(Physics.Raycast(myTrans.position, myTrans.forward, out hit, 50f))
       {
          //rayhit.text = "Locked On" ;
         
          caughtObj = hit.transform ;
          caughtObj.parent = myTrans ;
          caughtRig = caughtObj.rigidbody ? caughtObj.rigidbody : null ;
          if(caughtRig)
             caughtRig.isKinematic = true ;
       }
    }
 }

If this helps you I'll convert this post to an answer.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Raycasting from child weird behaviour 2 Answers

Make a simple tree 1 Answer

How can I differentiate between colliding with an object or its child? 1 Answer

How to make single parent of all the Childs and their child ? 1 Answer

How to return all the child gameobjects? 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