• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Refreshing the Polygon Collider (2D) upon sprite change? 1 Answer

How to shrink a line renderer 1 Answer

how back button works in unity calculator? 0 Answers

Making Top-Down spaceship movement, getting current speed, acceleration, without rigidbody! 1 Answer

I can't give damage with these scripts 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges