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 /
avatar image
0
Question by DanteAlighieri_ · Apr 24, 2018 at 02:46 PM · unity 2dpull

How to pull object with this code setup?

So I've been trying to parent an object to my player to be able to pull it. The pushing bit was obviously extremely easy. Yet the things I tried to pull with seemed to not work.

So I have an empty script call MovableBox which I attached to my box I want to move and called it in my colliders

if(collision.gameObject.GetComponent< MovableBox> () != null){canDrag = true; }

And I want to be able to grab box with control

if(Input.GetKey (KeyCode.LeftControl )&& canDrag == true){ }

Now what I used initially was along the lines of transform.parent = this transform.

But I failed to reference the movablebox properly since it didn't work at all.

Yes I am fairly new, but would appreciate some help.

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

Answer by Harinezumi · Apr 24, 2018 at 03:13 PM

There are many ways to do this, but for the way you approach it (parenting the player to the box), the first step is to access to object you will move:

 MovableBox movableBox = collision.gameObject.GetComponent<MovableBox>();
 if (movableBox != null) {
     canDrag = true;
     movableBox.transform.parent = transform;
 }

However, I would go about it very differently: instead of having a canDrag variable and making the movableBox a child, just store if you collided with a movable box (in a MovableBox variable), and if you are holding ctrl and you have a MovableBox, then any movement vector should also be applied to the movable box as well (you will probably want to make the MovableBox's rigidbody kinematic).

Btw, transform and this.transform are the same thing, so saying transform.parent = this.transform; is like Darth Vader saying "Luke, YOU are your father", which doesn't make sense (it might even give you an error).

Comment
Add comment · Show 3 · 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 DanteAlighieri_ · Apr 24, 2018 at 04:57 PM 0
Share

Yeah, I am still having trouble for some reason.

I'll try and figure it out.

But would you be as kind to elaborate on your suggested method a little bit?

avatar image DanteAlighieri_ DanteAlighieri_ · Apr 24, 2018 at 05:12 PM 0
Share

The particular problem I still have is a nullReferenceException

avatar image DanteAlighieri_ DanteAlighieri_ · Apr 24, 2018 at 05:29 PM 0
Share

I fixed it. Thank you.

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

80 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

Related Questions

Pulling in another object 1 Answer

adding Up force 0 Answers

Touch Detection in 2D Game 3 Answers

Add a force at an angle 1 Answer

Unity 4.3.4 2D mode, hide mouse pointer only works on half of the screen 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