Search code examples
c#unity-game-enginemouse-picking

When i pick an object from the ground, my script does something unexpected


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.


Solution

  • 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