0
\$\begingroup\$

I'm working on having a sticky conveyor belt, so that objects can also go around it. But my objects currently get stuck half way, as seen in the picture.

enter image description here

This is the movement code of the conveyor belt.

private void HandleCargo()
{
    var _speed = speed * Time.deltaTime;
    foreach (var cargo in _cargo)
    {
        var change = cargo.Key.transform.right * _speed;
        cargo.Key.transform.Translate(change);
    }
}

And the code taking care of the rotation to the surface.

private void Update()
{
    Debug.DrawRay(transform.position, -transform.up, Color.black);

    if (!Physics.Raycast(transform.position, -transform.up, out var hit, _distance)) return;
    var normal = hit.normal;

    transform.up -= (transform.up - hit.normal) * 0.05f;
}

If someone has pointers as to why this happens, that would be great. I kind of have an idea, since the transform right of the conveyor code is pointing inwards at this angles so the movement doesn't happen around the belt anymore, but I exhausted my attempts to fix it...

Edit: After DMGregory answer a new issue appeared. As soon as the cube starts to adjust to the spheric surface it is not exactly on the mesh anymore, which triggers my on collision exit code and stops movement on the object. I have some code that is supposed to stick the object to the belt, but that doesn't seem to work?

enter image description here

The sticky code

private void Update()
{
    Debug.DrawRay(transform.position, -transform.up, Color.black);

    if (!Physics.Raycast(transform.position, -transform.up, out var hit, _distance)) return;

    transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
    transform.position = new Vector3(transform.position.x, hit.point.y + 0.5f, transform.position.z);
}
\$\endgroup\$
0
\$\begingroup\$

Remember that transform.Translate operates in the object's local space by default.

So you either want to specify you're moving in world space:

var change = cargo.Key.transform.right * _speed;
cargo.Key.transform.Translate(change, Space.World);

...or simpler, just translate locally:

cargo.Key.transform.Translate(_speed, 0, 0);

As an aside, I'd recommend renaming your _speed variable to something like shift, offset, or travel. After multiplying by delta time to integrate it over the frame, it's no longer a rate of change over time (speed) — now it's an absolute increment.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your answer I adjusted accordingly, this sadly resulted in another issue where the cube is not touching the conveyor belt anymore and therefore triggers my on collider exit code which prevents it from moving, which is why I can't really test if your solution worked. \$\endgroup\$ Mar 10 '20 at 18:04
  • \$\begingroup\$ You might want to make your trigger a little larger than the contact area of your conveyor, or add a little hysteresis on your conveyor logic (so it keeps propelling an object until it exceeds a certain distance). \$\endgroup\$
    – DMGregory
    Mar 10 '20 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.