1

I'm currently working on a script that can pick up an item, and drop it with the click of the left mouse button. I'm also planning on addint rotating of an item and some icons to display whenever i do one of those actions.

I'm currently very new to this, so i might be trowing myself out in the depths. But i would like to try.

Here's my code:

public class PickUp : MonoBehaviour {

public Transform onHand;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    if(Input.GetMouseButton(1)){
        this.transform.position = onHand.position;
    }
}

void OnMouseDown () {



    GetComponent<Rigidbody>().useGravity = false;
    this.transform.position = onHand.position;
    this.transform.parent = GameObject.Find("Player").transform;
}

void OnMouseUp () {
    this.transform.parent = null;
    GetComponent<Rigidbody>().useGravity = true;
}

}

So far it kind of works.. I've some trouble picking up my object, it does not always let me. I have to click a couple of times, before it actually gets a hold on the object. When it does, the objects starts flying upwards for some weird reason i do not understand. I still have a hold on it, i can still walk around with it and as soon as i let go it falls down.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Athax
  • 75
  • 1
  • 6

1 Answers1

1

Based on the limited amount of data you have shown, I can suggest some common things to check / try. If you update with more details I'll try and help you further.

What value is assigned to your public Transform onHand variable?

Is there a reason why you are doing click detection in two places? Try deleting the lines inside the "Update" method.

The OnMouseDown method should be enough. However, for OnMouseDown to work, your object needs to have a physics collider setup. Check to see if you have a collider and that it's dimensions match what you would expect.

For debug purposes, try setting "isKinematic" to true on your pickup's rigidbody (from the inspector, permanently) this should disable gravity and other forces from moving your pickup object, so you can test out the rest of your code.

Also when you pick it up, set it's transform position to 0,0,0 This should make the object follow the player at the exact center spot of the player object.

Once you verify this works correctly, put features back in and see if any of them break the setup.

Start by setting the position back from 0,0,0 to onHand and then check if the pickup actually appears on the player's hand. If not, check the value of the onHand variable.

Then you can turn off isKinematic and see if everything is still okay.

As a side note: You might want to keep using isKinematic instead of disabling gravity. You could set it as kinematic when it's picked up to stop any forces from affecting the pickup's position, while the pickup itself will still have an effect on other rigidbodies. Then when you drop it on mouse up, just turn off isKinematic again

irreal
  • 2,217
  • 18
  • 23
  • The onHand variable holds a gameObject, that is the place where the object is going to be placed. Yes when i use Kinematic it stops flowing upwards, but a problem is when i drop it: it goes half way through the floor. – Athax Jan 21 '16 at 08:55
  • Have you tried all of the options I described? It's hard to debug your project with such limited info. The fact that it goes halfway through the floor could be due to a multitude of reasons. I'd need to see where the object was when you "dropped" it, how it's collider has been setup, how is the floor's collider setup, is there any extra code anywhere that deals with their positions or collider setups, if you move it back up above the floor through the editor, does it still fall through when you release it. etc. – irreal Jan 21 '16 at 09:01