Search Unity

Pulling a object using rigid body velocity?

Discussion in 'Physics' started by irjason, Sep 19, 2015.

  1. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    Hey guys,

    I have been working on a script and have come to a point that i am stuck on. I am trying to make it so the player can pull a block once it enters a trigger and a button is being pressed. The problem i am having is not so much the ability to pull the object but how the gravity / mass is effecting the rigidbody. I was wondering if there is a way to find out what the object's "drag" is. For instance i am setting the object's velocity to move at the same speed as the characters velocity in the OnTriggerStay2D.

    Code (CSharp):
    1. Vector2 moveSpeed = new Vector2(Controller.m_Rigidbody2D.velocity.x, 0f);
    the problem is that the block also has gravity and mass so i was wondering is there a way to calculate the y's current gravity / mass being pushed and get the drag amount of the object to apply to the pulling force of the object. Because at the moment though it does drag the box it will lose hold of the box as the velocity isn't quite enough due to the other factors involved.

    Also the reason i am trying to do it this way is so gravity will still take effect for example if i push it./pull over a ledge it will still fall as expected and leave the trigger area.

    Anyway thanks in advance for help if you want to more information or code from the script let me know.
     
  2. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    actually you can do

    pullObject.GetComponent<RigidBody2D>().velocity = new Vector2(Controller.m_Rigidbody2D.velocity.x, 0f);

    Still I would save RigidBody as a variable so you don't search for it every frame.