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...
SimpleMove()
function on character controller component \$\endgroup\$ – clzola Aug 24 '19 at 11:08