# Character Controller pulling Table in Unity Engine

I have setup a scene with one GameObject that has CharacterController component (Player) and another one Cube that represents Table with RigidBody and BoxCollider components.

Now, when I get close to this table I can press E and grab the table. Now I can pull or push table. What I do is when I press E key on keyboard I just attach this "table" to the Player gameobject with table.transform.SetParent(player.transform); Everything works great except when I add second cube that should prevent table to be moved any further.

As you can see, if I pull table in the direction of the arrow at some point, table will collide with blue cube. However, if I keep pressing S (for backward movement), player will continue to move, but table will not be moving (because I froze rotation on table's rigidbody component intentionally). What I want is to prevent player movement if the object that is currently being pushed or pulled cannot move any further...

Any ideas how to accomplish this?

EDIT:

public void PlayerMovement()
{
float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
float vertical = Input.GetAxis("Vertical") * MovementSpeed;

if (_isMovingObject)
{
horizontal = 0f;
float ppoDistance = Vector3.Distance(transform.position, _pushPullObject.transform.position);
float diffDistance = ppoDistance - _pushPullObjectDistance;

// distance between object and player is greater than at the start meaning pulling object
// bumped into collider while pulling backward, but we are now trying to move forward
// so wee need to restore diffDistance to 0
if (diffDistance > 0.001f && vertical > 0f)
{
//transform.position += transform.forward * diffDistance;
_pushPullObject.transform.position -= transform.forward * diffDistance;
}

// same thing but move backward after hitting obsticle while moving forward
if (diffDistance < -0.001f && vertical < 0f)
{
//transform.position -= transform.forward * diffDistance;
_pushPullObject.transform.position += transform.forward * diffDistance;

}

// stop backward movement if we hit something with pulling object
if (diffDistance > 0.001f && vertical < 0f)
vertical = 0f;

// stop forward movement if we hit something with pull/push object
if (diffDistance < -0.001f && vertical > 0f)
vertical = 0f;
}

Vector3 forwardMovement = transform.forward * vertical;
Vector3 rightMovement = transform.right * horizontal;

characterController.SimpleMove(forwardMovement + rightMovement);
}


This is what I came up with. I calculate original distance at the time of parenting table to Player object, then I keep track if that distance changed...

• Hey, How does the Character Controller help you, I've tried putting it on a object, but It didn't put any default movement on it or anything. – Millard Aug 23 '19 at 19:23
• @Millard You need to create Script that takes user input and use that to call SimpleMove() function on character controller component – clzola Aug 24 '19 at 11:08
• Thanks, I'll do that. – Millard Aug 24 '19 at 13:30
• @Millard youtube.com/watch?v=n-KX8AeGK7E This can help you a lot, watch the whole series ;) – clzola Aug 25 '19 at 20:22

Problem:
As it seems, your player as well as your table have each a rigidbody.
That means, that even though the table is part of player, it can move in local space, thus allow the player to move independendly.

Solution:
You can fix that relatively easy by removing the rigidbody from the table as soon as the player takes a hold of it.
Then the physics engine will recognize the table to be a part of the player's collider, thus any table collision will be in fact a player collision.

Note:
It's recommended not to move a rigidbody via transform.position = [...].
Instead use rigidbody.position to change the position.