简体   繁体   中英

Player behaves wierd when pickup Rigidbody2D object

I have a platformer 2D game made in Unity with c#. Player moves with rigidbody. I tried to make the player able to pickup an object. I found a old post here but wasnt able to comment on that. I can make it work without a rigidbody on the object but I want the object to bounch and slide away when dropping it. I wonder if anybody got a solution for this so I can have rigidbody on but objects.

Tested his script that looks like this:

  Collider2D touch = Physics2D.OverlapCircle(touchDetect.position, 0.01f, objectLayer);

if (Input.GetKey(KeyCode.LeftShift) && touch != null) 
{
    //grab on to the object
    touch.gameObject.transform.parent = this.transform;

    //if your box has a rigidbody on it,and you want to take direct control of it
    //you will want to set the rigidbody iskinematic to true.
   GetComponent<Rigidbody2D>().isKinematic = true;
} 
else if( touch != null)
{
    //let the object go
    touch.gameObject.transform.parent = null;

    //if your object has a rigidbody be sure to turn kinematic back to false
    GetComponent<Rigidbody2D>().isKinematic = false;
} 

Thanks

In my past I have had issues with using two objects that both have gravity enabled on their rigidbodies.

Try disabling the gravity on the rigidbody of the object the player is picking up. You can also temporarily disable the gravity on the object through code if the object needs to use it when it is not being interacted with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM