- Home /
How can you carry rigidbodies without them appearing jumpy?
I'm writing a script that would allow a player to "carry" an object similar to how objects are carried with the gravity gun in Half Life 2. Since these objects are quite big, I have to worry about them not clipping through the floor, so I can't just change the position. I have tried many approaches such as addForce and parenting, but the best way I have so far is changing the velocity on each step. But this has it's own problems as the box appears jumpy when walking, and collisions with walls makes the box go crazy. Here is what I have so far:
The carry loop (in C#):
IEnumerator carry(Transform obj) {
obj.rigidbody.useGravity = false;
while (carrying) {
Vector3 center = transform.position + (transform.forward* 4);
Vector3 objPos = obj.transform.position;
if (Vector3.Distance(objPos, center) > 1.5F) {
break;
} else if (Vector3.Distance(objPos, center) > .1F) {
Vector3 Dir = center - objPos;
obj.rigidbody.velocity = Dir * 50;
} else {
obj.rigidbody.velocity = Vector3.zero;
}
yield return null;
}
obj.rigidbody.velocity = obj.rigidbody.velocity * .5f;
obj.rigidbody.useGravity = true;
}
How it is called:
public void Update() {
//If pressed E and not carrying
if (Input.GetKeyDown(KeyCode.E) && !carrying) {
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 8)) {
if (hit.rigidbody && !hit.rigidbody.isKinematic) {
carrying = true;
StartCoroutine(carry(hit.transform));
}
}
} else if (Input.GetKeyDown(KeyCode.E) && carrying) {
carrying = false;
}
}
Any ideas on how to make this more smooth? Or is there a better way to do it rather than changing the velocity each call (which I know messes with the physics)
I am using a similar concept in my game, only it uses the mouse, but you might find it useful and implement it some way.
function On$$anonymous$$ouseDown(){
gameObject.Find("ObjectName").AddComponent(DragRigidbody);
}
function On$$anonymous$$ouseUp(){
Destroy(gameObject.Find("ObjectName").GetComponent(DragRigidbody));
}
You will need to import the DragRigidBody script first, Unity comes with this.
$$anonymous$$y experience with this script was that the boxes are to slow to react when you use a spring joint, like the DragRigidbody script did (unless you give it a high spring value which creates it's own problems). Perhaps I was configuring it wrong, but no matter what variables I gave it, I couldn't get it to work decently.
Answer by Owen-Reynolds · Apr 18, 2013 at 08:15 PM
The dragObject script connects a spring between the thing you are dragging and an invisible "drag point" (which would probably be a child in front of your hands.) Getting springs set correctly is a pain, would need to be sure to damp lots, but that could work.
For the setting velocity solution, I've gotten better results by not "snapping" to a target speed. Instead of vel=???
, use vel*=0.9f; vel+=???
. The 1st part is fake drag (and could maybe just set the rigidbody drag very high.) When you have to change directions, thqt turns a "snap" into a few frames of slowing to reverse.
Getting the correct values is ugly. For example, with a drag of 10% (speed times 0.9) adding a speed of 5 will reach speed 50 (at speed 50 the 10% drag and the +5 cancel.) Values like times 0.75 (and more speed) will make it stiffer.
Unfortunately, I couldn't find a value for the fake drag that stopped the erratic behavior of the box. Low values of drag gave the same jumpy result, while high values created too much velocity.
While walking? Does moving forwards give sort a delayed jerk to the object? $$anonymous$$aybe also add all (or some) or the player's movement.
Code drags from 0.99 to 0.9 are interesting. Anything lower (more than 10%/frame) is basically just a $$anonymous$$oveTowards.
One more trick that works in some situations is to lower the push (or increase the drag) when close to the target area, and stop movement when "close enough."
Your answer
Follow this Question
Related Questions
move 2d character affected by physics with velocity and/or add force 2 Answers
Physics: Auto-stopping a spaceship with reverse thrusters 1 Answer
Player slows down when jumping/velocity changes 1 Answer
How to move a gameObj by cicking/ touching another gameobj? 0 Answers
Constraining maximum movement speed on a rigidbody on certain axes. 1 Answer