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
0
Question by ChrisSch · Aug 17, 2013 at 02:27 PM · collisionfunctionparentkeypress

Simple Parenting Question

Hi everyone! :D

Ok so I made a simple trigger script where when the space ship enters the trigger it can pick up the object by pressing and holding F (at least I think I did the input code right), and when its released the object drops.

Now this script is on all pickupable objects, and the object checks if the object entering and exiting the trigger is tagged "Player" or "ShipLoaded" and executes the PickUp() and Drop() functions.

My question is how to in those codes make the object holding that script child of the object with one of those tags in the PickUp() function?

 #pragma strict
 
 var canPickUp : boolean = false;
 
 function OnTriggerEnter(coll:Collider)
 {
        if(coll.gameObject.tag == "Player" || "ShipLoaded")
        {
         canPickUp = true;
        }
        else
        {
            canPickUp = false;
        }
 }
  
 function OnTriggerExit(coll:Collider)
 {
     canPickUp = false;
 }
  
 function Update()
 {
     if(Input.GetKey("f") && canPickUp)
     {
         PickUp();
     }
     else if (Input.GetKeyUp("f") && canPickUp)
     {
         Drop();
     }
 }
 function PickUp()
 {
     //parenting code goes here
 }
 
 function Drop()
 {
     //unparenting code goes here
 }
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 YoungDeveloper · Aug 17, 2013 at 02:44 PM 0
Share

Hi, define what do you mean with "pick up". How do you want it to be picked up ? Everybody can see the picked object, it's on a rope or something, or the object just disappears on f hold, and spawns again when you release the f button.

avatar image ChrisSch · Aug 17, 2013 at 02:46 PM 0
Share

By pick up I mean hold, not disappear just hold, parented to the object with tags "Player" or "ShipLoaded" and moves the way its parent moves.

avatar image Fattie · Aug 17, 2013 at 03:31 PM 0
Share

When I clicked on this question, I thought it was going to say something like: "What advice should I give my teenage daughter about homework..." heh :)

avatar image ChrisSch · Aug 17, 2013 at 04:05 PM 0
Share

Lmao Fattie. xD Not that kind of parenting. xD

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JoaquinRD · Aug 17, 2013 at 02:51 PM

Use transform.parent = coll.transform;

Try this:

 #pragma strict
  
 function OnTriggerStay(coll:Collider)
 {
     if(tranform.parent == null && (coll.gameObject.tag == "Player" || coll.gameObject.tag == "ShipLoaded") && Input.GetKey("f"))
     {
        transform.parent = coll.transform;
     }
 }
  
 function Update()
 {
     if (Input.GetKeyUp("f"))
     {
        tranform.parent = null;
     }
 }
Comment
Add comment · Show 8 · 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 JoaquinRD · Aug 17, 2013 at 02:55 PM 0
Share

By the way, using the or (||) operator will not work with two strings, such as:

 if(coll.gameObject.tag == "Player" || "ShipLoaded")

You have to compare each of them separately:

 if(coll.gameObject.tag == "Player" || coll.gameObject.tag == "ShipLoaded")
avatar image ChrisSch · Aug 17, 2013 at 04:02 PM 0
Share

Thank you for that! I didn't know that, and I forgot I can put everything in the if statement even tho I know how. xD

Anyway, it didn't work at first but then I changed it a bit so it works kind of now. When I press F it gets parented but it doesn't move exactly like its parent. In fact it barely moves at all. Did I misunderstand the concept of parenting? I thought parenting means the child inherits all of its parents attributes, including script functions for movement, for example.

Here is the new version of the code:

 #pragma strict
  
 function OnTriggerStay(coll:Collider)
 {
     if(transform.parent == null && (coll.gameObject.tag == "Player" || coll.gameObject.tag == "ShipLoaded") && Input.Get$$anonymous$$ey("f"))
     {
         rigidbody.useGravity = false;
         rigidbody.mass = 1;
         transform.parent = coll.transform;
         Debug.Log("Picked up!");
     }
 }
 
 function OnTriggerExit(coll:Collider)
 {
     transform.parent = null;
     Debug.Log("Dropped it D:");
     rigidbody.useGravity = true;
     rigidbody.mass = 5;
 }
  
 function Update()
 {
     if (Input.Get$$anonymous$$eyUp("f"))
     {
         transform.parent = null;
         Debug.Log("Dropped it D:");
         rigidbody.useGravity = true;
         rigidbody.mass = 5;
     }
 }
avatar image JoaquinRD · Aug 17, 2013 at 04:23 PM 2
Share

Ah, I did not consider that the pickup might use physics. Ins$$anonymous$$d of setting useGravity and mass, simply set is$$anonymous$$inematic to true when picked up and false when dropped. This will make sure that the pickup is securely attached because it will ignore all physics and just follow its parent.

Also, I don't think you'll need OnTriggerExit.

avatar image ChrisSch · Aug 17, 2013 at 05:56 PM 0
Share

Ok we're almost there, its working now, there's one more thing left now. When, while holding the object, the object collides with something it just goes through it. I attempted to make it unparent when it does but nothing happens its still goes trough. :S

 #pragma strict
  
 function OnTriggerStay(coll:Collider)
 {
     if(transform.parent == null && (coll.gameObject.tag == "Player" || coll.gameObject.tag == "ShipLoaded") && Input.Get$$anonymous$$ey("f"))
     {
         rigidbody.is$$anonymous$$inematic = true;
         transform.parent = coll.transform;
         Debug.Log("Picked up!");
     }
 }
  
 function Update()
 {
     if (Input.Get$$anonymous$$eyUp("f"))
     {
         transform.parent = null;
         Debug.Log("Dropped it D:");
         rigidbody.is$$anonymous$$inematic = false;
     }
 }
 
 function OnCollisionEnter(coll:Collider)
 {
     transform.parent = null;
     rigidbody.is$$anonymous$$inematic = false;
 }
avatar image JoaquinRD · Aug 17, 2013 at 06:10 PM 0
Share

This is happening because its collider is s trigger. OnCollisionEnter will only be called if the collider is not a trigger.

Show more comments

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

How to put two game objects together and disable rigidbody on a child? 1 Answer

Player object still gets destroyed even when shields up 1 Answer

i cant get an object to become child of an other JS 1 Answer

OnCollisionEnter(Collision Other) setActive affecting parent 1 Answer

Is calling OnCollisionEnter from multiple scripts bad? 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