درس: منصة متحركة

منصة تنزلق بين نقطتين، وتتوقف عند كل طرف، وتحمل ما يقف عليها.

1. الإعداد

الإعداد

اصنع Cube مسطحًا له collider وضعه حيث يجب أن تبدأ. لا حاجة إلى شيء آخر.

2. السكربت

السكربت

Scripts/MovingPlatform.cs

public class MovingPlatform : Behaviour
{
    public Vector3 travel = new Vector3(6f, 0f, 0f);
    public float secondsEachWay = 3f;
    public float waitAtEnds = 1f;

    Vector3 home;
    Vector3 last;

    public override void Start()
    {
        home = transform.position;
        last = home;
        Go(true);
    }

    void Go(bool out_)
    {
        // one tween each way; when it ends, the next starts after the wait
        Tween.Move(gameObject, out_ ? home + travel : home, secondsEachWay, "inout").After(waitAtEnds).Then(() => Go(!out_));
    }

    public override void LateUpdate()
    {
        last = transform.position;
    }

    public override void OnCollisionStay(GameObject other)
    {
        // whatever rests on the platform moves by what the platform moved this frame
        Vector3 moved = transform.position - last;
        if (other.transform.position.y > transform.position.y) other.transform.Translate(moved);
    }
}

احفظه باسم Scripts/MovingPlatform.cs وأضفه إلى المنصة.

3. كيف يعمل

كيف يعمل

4. جرّبه

جرّبه

اضغط PLAY واقفز عليها. اجعل travel تساوي (0, 5, 0) للحصول على مصعد، وغيّر waitAtEnds لتوقفات أطول.

التالي: نقاط الحفظ.