x


Aligning and moving a gameObject down the center of another's axis (Conveyor Belt)

Hi, I'm trying to make a simple conveyor belt system where the objects on the belts run down the center of the belt. My problem occurs when the object reaches a belt that has a different rotation.

How can I smoothly move an object from one belt to another while getting them to center on a belt's Z-axis?

This is code I'm using to move rigidbodies that collide with the belt:

void OnCollisionStay(Collision collision) { //An object is on the conveyor
    if (collision.rigidbody) {
        collision.rigidbody.MovePosition(collision.rigidbody.position + collision.transform.InverseTransformDirection(transform.forward) * speed * Time.deltaTime);

    }

}


My conveyor setup looks like this: Valid XHTML

Thanks!

more ▼

asked Dec 22 '10 at 05:59 PM

CPUFreak91 gravatar image

CPUFreak91
3 1 1 5

Answers are accepted by chackmarking them, not writing "solved" in the title.

Dec 22 '10 at 10:02 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Because you're already using a Rigidbody, you could use collision.rigidbody.AddForce(...) instead of collision.rigidbody.MovePosition(...), and then let the physics engine take care of it.

Alternatively, the conveyer belt could make itself the parent of the rigidbodies that hit it, so that they would then use its transform coordinates. That would force the children objects to align with whatever conveyer they hit last.

collision.transform.parent = transform;

more ▼

answered Dec 22 '10 at 07:18 PM

Mickydtron gravatar image

Mickydtron
357 1 9

Thanks for your reply!

I experimented with the AddForce method, but with rigidbodies with rectangular shapes just "roll" and bounce on the conveyor belts.

I could parent objects to the conveyor belt, but I still don't know how to center them over the z-axis, especially smoothly over time.

Dec 22 '10 at 07:31 PM CPUFreak91

well, because children apply their transform relative to their parents, if the rigidbodies transform z value is 0, it will be centered on its parents z axis. As for smoothing it, you could find the vectors for where its velocity will be taking it and where you would truly want it to be, and then Lerp() them and apply that result. Possibly. I'm not the best at smoothing things.

Dec 22 '10 at 07:38 PM Mickydtron

Also, in regards to the rigidbody.AddForce, it should work better for your purposes to say collision.rigidbody.AddForce(someNumber, ForceMode.VelocityChange); (I think)

See http://unity3d.com/support/documentation/ScriptReference/ForceMode.html for where I got that from. I believe that this force mode just changes the velocity directly, so you could set the velocity to be whatever you want.

Dec 22 '10 at 07:44 PM Mickydtron

transform.localPosition is the variable to look for.

Dec 22 '10 at 09:01 PM Mickydtron

Thanks for your help Mickydtron. I got it to work by parenting and using localPosition. I set the rigidbody's parent to null when it leaves a belt segment in OnCollisionExit() and then if it is colliding with another belt and its parent is null, it will then re-parent to the new belt. This allows the objects to transfer completely to the next belt before it applies a MovePosition which "centers" it enough. Then I can tweak it a bit using transform.localPosition.

Dec 22 '10 at 09:59 PM CPUFreak91
(comments are locked)
10|3000 characters needed characters left

I made up a little example project.

I use a low friction physics material (low friction only in one direction like ski's) It adds forces to the touching objects in the direction of the conveyor.

Fiddle with the conveyor 'physic material' dynamic frictions and the Conveyor components speed variables. The visual scrolling is UV scrolling, this is seperate from the forces applied to the other objects

Get it here http://users.on.net/~edan/cam/visible/Conveyor.zip

more ▼

answered Mar 29 '11 at 08:41 AM

Vectrex gravatar image

Vectrex
131 1 2 6

(comments are locked)
10|3000 characters needed characters left

Here's the conveyor belt force operator that worked for me: //Push on things which collide with this floor var speed:int = 100; function OnCollisionStay(collision : Collision) { //An object is on the conveyor if (collision.rigidbody) {

    var tilt : Vector3 = Vector3.right;
    var roty = transform.parent.transform.rotation.y;

    //Fudged this to get conveyors to push in the right direction
    if(roty==0.7071068){
        tilt = Vector3.back;
    }else if(roty==-0.7071068){
        tilt = Vector3.forward;
    }else if(roty==0){
        tilt = Vector3.right;
    }else {
        tilt = Vector3.left;
    }
    //Try other Forcemodes
    collision.rigidbody.AddForce(tilt * speed * Time.deltaTime,  ForceMode.VelocityChange);
}

}

more ▼

answered Feb 04 '12 at 02:14 PM

TheBelin gravatar image

TheBelin
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x92
x5

asked: Dec 22 '10 at 05:59 PM

Seen: 1739 times

Last Updated: Feb 04 '12 at 02:14 PM