0
\$\begingroup\$

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.

enter image description here

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...

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – Millard
    Aug 23 '19 at 19:23
  • \$\begingroup\$ @Millard You need to create Script that takes user input and use that to call SimpleMove() function on character controller component \$\endgroup\$
    – clzola
    Aug 24 '19 at 11:08
  • \$\begingroup\$ Thanks, I'll do that. \$\endgroup\$
    – Millard
    Aug 24 '19 at 13:30
  • \$\begingroup\$ @Millard youtube.com/watch?v=n-KX8AeGK7E This can help you a lot, watch the whole series ;) \$\endgroup\$
    – clzola
    Aug 25 '19 at 20:22
-1
\$\begingroup\$

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.
See the API for more information.

\$\endgroup\$
3
  • \$\begingroup\$ Player does not have Rigidbody component, it can't have it because it has Character Controller component. But if it does, then I think better way would be to connect table and player via Joint? \$\endgroup\$
    – clzola
    Aug 24 '19 at 11:09
  • \$\begingroup\$ Yes. I think in that case a fixed joing might be the best solution. \$\endgroup\$ Aug 24 '19 at 11:18
  • \$\begingroup\$ But still I do not have rigid body on character, I am using character controller, do you have any suggestions? \$\endgroup\$
    – clzola
    Aug 25 '19 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.