Search code examples
c#unity-game-enginecollision

Collider not working if object is not moving


I tryed to make an elevator and i came around something that i could not explain.

So here is the scenario:

Once i put my object(in this case a cube) on the elevator it will go up and down. If the object sits out the ride without moving and waits for the next ride, the colliding will stop when the elevator comes back through the ground.

If the object moves while the elevator come's back everything is fine and the object go's for another ride.

Could someone explain to my why this is happening and is there a fix for this ?

The platform is expected to go through the ground and pick up the object once it comes back above ground. (Imagine a not all to sectret elevator)

I alredy tried to add the script DontGoThroughThings.cs. It didnt work either.

Here is a screenshot of the inspectors. enter image description here

Here is the script i made for the elevator to go up and down.

Elevator.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Elevator: MonoBehaviour {

    public float speed;
    public float distance;

    private bool goingUp;

    void Start () {
        goingUp = true;
    }

    void Update () {
        var currentPosition = transform.position;
        currentPosition.y = goingUpDown(currentPosition.y);
        transform.position = currentPosition;
    }

    private float goingUpDown(float currentPosition)
    {
        if (goingUp)
            currentPosition += speed;
        else
            currentPosition -= speed;

        if (currentPosition > distance / 2)
            goingUp = false;
        if (currentPosition < -distance / 2)
            goingUp = true;
        return currentPosition;
    }
}

If you need any more information leave a comment. Thank you


Solution

  • You need to add a RigidBody to your elevator object, and then check the "Is Kinematic" option on it.

    From the Unity Documentation RigidBody Manual:

    Is Kinematic : If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached.