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 FearofWonderland · Dec 17, 2013 at 10:24 PM · collisionobjectattach

How to have an object attach to my player when collided

When my player walks into this object, in this case a cube, I need it to simply attach to the player as if they're carrying it. This is what I've done so far.

 var blockTransform : Transform;
 var playerTransform : Transform;
 
 function OnCollisionEnter (collision : Collision)
 {
     if(collision.transform.name == ("Player"))
     {
         blockTransform.transform.parent = playerTransform.transform;
         }
         }
Comment
Add comment · Show 1
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 iwaldrop · Dec 18, 2013 at 06:54 AM 0
Share

If Tim's answer works for you, please accept it as correct. Otherwise, provide more information about your problem or describe why his solution doesn't work for you.

2 Replies

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

Answer by FearofWonderland · Dec 18, 2013 at 08:35 PM

I've got the solution now. To start with, the character controller is too large so no collision can be detected. Then, this code...

Put this on the player:

 #pragma strict
 
 var blockTransform : Transform;
 var playerTransform : Transform;
 var holdSlot : Transform;
 
 function OnCollisionEnter (collision : Collision)
 {
 if(collision.transform.name == ("Block"));
 {
 blockTransform = collision.transform;
 blockTransform.transform.parent = transform;
 blockTransform.transform.position = holdSlot.transform.position;
 }
 }

The block gets translated to infront of the player, or where ever the holdSlot is placed automatically when the player touches it.

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 iwaldrop · Dec 19, 2013 at 05:13 AM 1
Share

I hope your actual code is properly indented. If not, I would hate to work with code base, and you'd probably find that I formatted it all. :)

avatar image devinkid12 · Oct 17, 2016 at 12:24 AM 0
Share

I do not actully know what you are meaning AND it doesent work for me, Is It Javascript Or C#? I had put the script in the holdSlot GameObject (E$$anonymous$$PTY) but when the holdSlot touched the cube named Block it didnt do anything, Replying would be helpful.

avatar image
1

Answer by TimBorquez · Dec 17, 2013 at 10:57 PM

you could have an empty game object as a child of the player and make a variable for it like holdSlot, then move your object to the spot like

 blockTransform.transform.position = holdSlot.transform.position;

i would in most cases make picking up stuff happen from code on the player not from the thing you pick up though

Comment
Add comment · Show 5 · 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 FearofWonderland · Dec 18, 2013 at 12:42 PM 0
Share

I altered the code to look like this.

 function OnCollisionEnter (collision : Collision)
 {
     if(collision.transform.name == ("Block"))
     {
         blockTransform.transform.parent = playerTransform.transform;
         blockTransform.transform.position = holdSlot.transform.position;
         }
         }

But it still isn't working, I don't know what I could be doing wrong.

avatar image TimBorquez · Dec 18, 2013 at 04:40 PM 0
Share

umm did you make a variable for your hold slot object and set it in the inspector?

avatar image FearofWonderland · Dec 18, 2013 at 04:58 PM 0
Share

Yeah. Don't know why it isn't working.

avatar image TimBorquez · Dec 18, 2013 at 06:47 PM 0
Share

well i see that you're using a name to detect the right object, did you make sure the object is named correctly

also check to see if it's even going into the collision function with a Debug.Log();

avatar image FearofWonderland · Dec 18, 2013 at 06:58 PM 0
Share

Nope, no kind of collision being detected.

 #pragma strict
 
 var blockTransform : Transform;
 var playerTransform : Transform;
 var holdSlot : Transform;
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function OnCollisionEnter (collision : Collision)
 {
 if(collision.transform.name == ("Block"));
 {
 Debug.Log("yes");
 blockTransform.parent = playerTransform.transform;
 blockTransform.transform.position = holdSlot.transform.position;
 }
 }

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

20 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

Related Questions

place a object on the position where the object is colliding with the terrain. 1 Answer

Collision with a coin 0 Answers

Unparent object on collision? 3 Answers

Lowering impact between 2 objects 1 Answer

Attaching objects on collision 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