[unity2d] teaches you how to make an automatic lifting platform

       In the 2D games we played when we were young, such as Super Mario, Contra, etc., there are often lifting platforms on the map. Without further ado, the following is the process for bloggers to realize the automatic lifting platform:

        The first step is to create a new Tilemap (as shown in the figure below, the material can be searched for Sunnyland in the Unity Asset Store, and you can import it), and build a platform, and set the Layer of the (Inspector) inspection panel to Ground (that is, the ground, adjust according to your own project settings) , add Tilemap Collider 2D and Rigidbody 2D to the platform, set the Body Type to Kinematic (I will introduce it at the end of the article), and lock the X-axis and Z-axis. Note that if you add Box Collider 2D, you need to edit its collision body range, Tilemap Collider 2D solves this problem very well.

        The second step is to create two empty objects (named top and bottom respectively) in the platform sub-level. For the convenience of observation, you can set the color mark in the empty object inspection panel (as shown in the figure below), and then move the two empty objects, respectively, to the The distance directly above and below the platform can be set freely, so that the environment we need is set up.

      The third step is to paste the code on the platform, create a new C# file, and name it PlatformController. The blogger’s idea is to obtain the top and bottom points (critical points), and judge whether the state of the platform is moving up or down. The default upward speed is Speed , when the platform moves down, the speed is -Speed. Of course, this is only one implementation method. In the next article, the blogger will take the 3D automatic lifting platform as an example to implement it with another method. Interested friends are welcome to check it out, PlatformController The .cs code is as follows:

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

public class PlatformController : MonoBehaviour
{
    private Rigidbody2D rb;
    private Collider2D coll;
    public Transform top, bottom;
    public float Speed;
    public float TopY, BottomY;
    private bool isUp = true;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<Collider2D>();
        TopY = top.position.y;//获取top点
        BottomY = bottom.position.y;//获取bottom点
        Destroy(top.gameObject);
        Destroy(bottom.gameObject);
        /*破坏top和bottom物体,使运行后层级面板更美观*/
    }

    // Update is called once per frame
    void Update()
    {
        Movement();
    }
    void Movement()
    {
        if (isUp)
        {
            rb.velocity = new Vector2(rb.velocity.x, Speed);
            if (transform.position.y >= TopY)
            {
                isUp = false;
            }
        }
        else
        {
            rb.velocity = new Vector2(rb.velocity.x, -Speed);
            if (transform.position.y <= BottomY)
            {
                isUp = true;
            }
        }
    }
}

       Before running, don't forget to get Top and Bottom in the inspection panel Script, and set the speed!

There are three attributes of Body Type:

  • Dynamic (dynamic, default)
  • Kinematic
  • Static

       As for why you want to change the Body Type, you can try to set it to Dynamic first. When you jump up and land on the platform, observe carefully, and you will find that the platform has moved down a certain distance due to the impact of the character’s fall. When we set it to Static, the platform will not move under the control of the code. Only when it is set to Kinematic, the movement of the platform will not be affected by other factors (effect of force). Interested partners can refer to the information for further study and understanding.

Guess you like

Origin blog.csdn.net/m0_51942776/article/details/127603535