How to make a conveyor belt?

Hey,

Does anybody know a good way to make a conveyor belt in Unity, preferably using JavaScript?

Any help would be great!
Thanks!

If referring to how to make the conveyor belt move the player along, something like the following should work (with rigidbody attached):

C# syntax, I have not used the Unity flavour of JavaScript yet, but the following may be of use to you.

public class ConveyorBelt : MonoBehaviour {

    public float speed = 3.0f;

    void OnCollisionStay(Collision collision) {
        if (collision.gameObject.tag != "Player")
            return;

        // Assign velocity based upon direction of conveyor belt
        // Ensure that conveyor mesh is facing towards its local Z-axis

        float conveyorVelocity = speed * Time.deltaTime;

        Rigidbody rigidbody = collision.gameObject.rigidbody;
        rigidbody.velocity = conveyorVelocity * transform.forward;
    }

}

Also, if you want to move objects that touch it, take a look on [Rigidbody.AddForce()][1]

I know this thread is old, but maybe someone could help me?

I’ve used the code above and it works fine, however the box doesn’t ever stop accelerating, till it goes flying off the end of the conveyor belt. This is not the behavior I desire.

How would I get the box to move at a constant(ish) speed?

Hello, I made a conveyor belt asset that you might like to take a look at.

Has a moving texture and it moves rigidbody objects that drop on it.
Would love some feedback on if it’s what people are looking for, or if it needs some improvements.

public float speed;

void OnCollisionStay(Collision obj)
{
    float beltVelocity = speed * Time.deltaTime;
    obj.gameObject.GetComponent<Rigidbody>().velocity = beltVelocity * transform.forward;
}

This worked very well for me

An easier way for a 2D conveyor best now would be with the Surface Effector 2D, as creating an object and attaching this component allows you to make one.

The steps would be the following;

Add a box collider and the effector.
In the box collider, make sure that you tick ‘Used By Effector’ and uncheck all the others.
Then in the surface Effector decide on your speed and direction with ‘Speed’, which you can go forward with a positive number and backwards with a negative.

This should create a simple conveyor belt for a simple platformer game.

Also if you want the conveyor belt to change speed/ direction randomly you can do the following.
Going into the surface effector and choosing the speed you want the belt to go in the ‘Speed Variation’ section.