- Home /
Issues when using Rigidbody force to Move/Carry Objects
Hi all, I am having some issues when changing my Object Carrying Method from a Static Locked position attached to the player to a more fluent movement that also has physics so carried objects cannot phase through walls etc. The issue I am facing while using this is that the object if placed under the Player and then picked up it levitates the player up into the sky, is there a way that while an object is being carried it ignores only the players collider, while still being able to crash into walls and other objects?
The other issue I am having with this is that the object never seems to settle at the carry position, it always overshoots it's mark and is constantly 'bouncing' back and forth around the carrying position.
The coding I am using to move the object toward its position while being carried is as follows:
void moveObject()
{
if (isSmallOBJ)
{
if (transform.position != smallDest.position)
{
if(distance < 0.05f)
{
Vector3 f = smallDest.position - transform.position;
f = f.normalized;
f = f * lowForce;
rig.AddForce(f);
distance = Vector3.Distance(transform.position, smallDest.position);
}
else if (distance < 0.5f)
{
Vector3 f = smallDest.position - transform.position;
f = f.normalized;
f = f * midForce;
rig.AddForce(f);
distance = Vector3.Distance(transform.position, smallDest.position);
}
else if (distance < 5f)
{
Vector3 f = smallDest.position - transform.position;
f = f.normalized;
f = f * highForce;
rig.AddForce(f);
distance = Vector3.Distance(transform.position, smallDest.position);
}
else
{
dropObject();
}
}
}
else
{
if (distance < 0.05f)
{
Vector3 f = dest.position - transform.position;
f = f.normalized;
f = f * lowForce;
rig.AddForce(f);
distance = Vector3.Distance(transform.position, dest.position);
}
else if (distance < 0.5f)
{
Vector3 f = dest.position - transform.position;
f = f.normalized;
f = f * midForce;
rig.AddForce(f);
distance = Vector3.Distance(transform.position, dest.position);
}
else if (distance < 5)
{
Vector3 f = dest.position - transform.position;
f = f.normalized;
f = f * highForce;
rig.AddForce(f);
distance = Vector3.Distance(transform.position, dest.position);
}
else
{
dropObject();
}
}
}
Showcasing the Issues
Answer by DenisIsDenis · May 22 at 04:50 AM
UnityEngine has a function:
Physics.IgnoreCollision(colliderPlayer, colliderObject, true);
to disable collision of these two colliders, and:
Physics.IgnoreCollision(colliderPlayer, colliderObject, false);
to enable collision of these two colliders.
When you pick up an object, you need to use the first function for all players and all colliders of the lifted object, and the second function - when throwing the object.
The moveObject()
function code can be simplified:
void moveObject()
{
if(isSmallOBJ)
{
if (transform.position != smallDest.position)
{
if (distance < 5)
{
rig.velocity = Vector3.zero;
rig.MovePosition(Vector3.Lerp(rig.position, smallDest.position, Time.deltaTime * 15));
}
else
{
dropObject();
}
}
}
else
{
if (distance < 5f)
{
rig.velocity = Vector3.zero;
rig.MovePosition(Vector3.Lerp(rig.position, dest.position, Time.deltaTime * 15));
}
else
{
dropObject();
}
}
}
In this code, the object does not tremble in different directions.
Also, the MovePosition()
function takes into account physics, so the interaction with other objects will be correct.
I truly don't know what the heck is going on with this website, the last 2 weeks its near impossible to even post a question for the last week or more I've been trying to reply to this answer and the stinking website does nothing, I click 'Submit' to add my comment and the button then greys out and does nothing, this is so frustrating, what's the bet it actually lets this comment post?!??!
ofcourse -.- SIGH.... Im going to post the comment I've been trying to post for the last week in a paste bin link, this is ridiculous it just will not let me post it >:(
True, my method does not allow you to throw an object. But you can disable the collider for all child objects in the loop:
void SetCollisionState(bool isDisableCollision)
{
var collidersOfObject = rig.GetComponentsInChildren<Collider>();
var collidersOfPlayer = this.GetComponentsInChildren<Collider>();
foreach (var playerCollider in collidersOfPlayer)
{
foreach (var objectCollider in collidersOfObject)
{
Physics.IgnoreCollision(objectCollider, playerCollider, isDisableCollision);
}
}
}
This code loops through all the object's colliders with all the player's colliders and turns collision on or off.
Your answer

Follow this Question
Related Questions
AddForce to position on Rigidbody 2 Answers
Confusion with AddForce with ForceMode.Acceleration 1 Answer
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
raycast hit add force problem, help needed please 1 Answer
Calculate forces required to rotate around an arbitrary point regardless of center of mass position 1 Answer