picking up object morphs object's shape, how do i stop this?

I have two problems i have written some code to pick up a box by pressing the q key. if i walk up to the box and look directly down at it and press q it shoots the player up into the air and off the map. The second issue is the box starts off as a nice square as i rotate and jump etc it morphs the objects shape some how because when i press q to release the box it is a different shape (different everytime i test it).

any ideas how to fix these issues???

heres my code:

var SpawnTo : Transform; //your hand for example, attack an object to your character that you want the position of what you picked up to go to
var Object1 : Transform; //what your picking up, the object that you want to move
var dist = 5; //distance at which you can pick things up
var carryOffset : Vector3; // position of a carried object relative to the camera
private var isHolding = false;


function Update () {
    if(Input.GetKeyDown(KeyCode.Q)){ //if you press 'q'
        if(Vector3.Distance(transform.position, Object1.position) < dist) //if distance is less than dist variable
        {
            isHolding = !isHolding; //changes isHolding var from false to true 
            }
            }

    if(isHolding == true){
        Object1.rigidbody.useGravity = false; //sets gravity to not on so it doesn't just fall to the ground

        Object1.parent = SpawnTo; //parents the object
        Object1.rigidbody.isKinematic = true; // make object kinematic
        Object1.collider.isTrigger = true;

        Object1.transform.position = SpawnTo.transform.position + carryOffset; //sets position

        Object1.transform.rotation = SpawnTo.transform.rotation; //sets rotation
        }
        else{ //if isHolding isn't true
            SpawnTo.transform.DetachChildren(); //detach child (object) from hand
            Object1.rigidbody.useGravity = true; //add the gravity back on
            Object1.rigidbody.isKinematic = false;// turn off kinematic
            Object1.collider.isTrigger = false;
        } 
}

The first problem - player thrown away when picking some near object - may be caused by the way you’re defining Object1: I suspect that in certain cases the player picks itself instead of another object; since you offset the picked object by carryOffset, this starts a “dog chasing the tail” infinite loop which throw the player in the offset direction. If this is the problem, it can be solved by picking only objects with a certain tag (not the tag “Player”, for sure!)

The second problem - box morphed after being released - probably is caused by parenting/unparenting. If the parent object has non-uniform scale - (0.5,1,0.5), for instance - and a non-zero rotation, the box scale isn’t correctly restored when unparented (smells like a Unity bug for me!).

You can avoid this parenting problem using another approach: instead of childing the object to SpawnTo, make it follow the SpawnTo position in Update:

var SpawnTo : Transform; //your hand for example, attack an object to your character that you want the position of what you picked up to go to
var Object1 : Transform; //what your picking up, the object that you want to move
var dist = 5; //distance at which you can pick things up
var carryOffset : Vector3; // position of a carried object relative to the camera
// Substitute the flag isHolding by the variable below:
private var pickedObj: Transform; // picked object transform, null if nothing picked

function Update () {
    if(Input.GetKeyDown(KeyCode.Q)){ //if you press 'q'...
        if (pickedObj){ // and something picked...
            pickedObj.rigidbody.isKinematic = false; // drop object to ground
            pickedObj.collider.isTrigger = false;
            pickedObj = null; // signals that nothing picked anymore
        }
        else // but if nothing picked yet and distance less than dist variable...
        if (Vector3.Distance(transform.position, Object1.position) < dist)
        {
            pickedObj = Object1; // signals the object is picked
            pickedObj.rigidbody.isKinematic = true; // disable physics control
            pickedObj.collider.isTrigger = true;
        }
    }
    // if something picked, make it follow SpawnTo
    if (pickedObj){ 
        pickedObj.position = SpawnTo.TransformPoint(carryOffset); // keeps the offset relative to SpawnTo
        pickedObj.rotation = SpawnTo.transform.rotation; // keeps same rotation too
    }
}

thanks this definitely solved the issue with the morphing of the object/box. due to time i had make a little work around for the first issue and just increased the distance that i can pick the object up from. i will try tagging though.

thanks again :slight_smile: