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


i'm working on script can pick item, , drop click of left mouse button. i'm planning on addint rotating of item , icons display whenever 1 of actions.

i'm new this, might trowing myself out in depths. try.

here's code:

public class pickup : monobehaviour {  public transform onhand;  // use initialization void start () {  }  // update 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 kind of works.. i've trouble picking object, not let me. have click couple of times, before gets hold on object. when does, objects starts flying upwards weird reason not understand. still have hold on it, can still walk around , let go falls down.

based on limited amount of data have shown, can suggest common things check / try. if update more details i'll try , further.

what value assigned public transform onhand variable?

is there reason why doing click detection in 2 places? try deleting lines inside "update" method.

the onmousedown method should enough. however, onmousedown work, object needs have physics collider setup. check see if have collider , it's dimensions match expect.

for debug purposes, try setting "iskinematic" true on pickup's rigidbody (from inspector, permanently) should disable gravity , other forces moving pickup object, can test out rest of code.

also when pick up, set it's transform position 0,0,0 should make object follow player @ exact center spot of player object.

once verify works correctly, put features in , see if of them break setup.

start setting position 0,0,0 onhand , check if pickup appears on player's hand. if not, check value of onhand variable.

then can turn off iskinematic , see if still okay.

as side note: might want keep using iskinematic instead of disabling gravity. set kinematic when it's picked stop forces affecting pickup's position, while pickup still have effect on other rigidbodies. when drop on mouse up, turn off iskinematic again


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -