Unity beginner 3 - the enemy's movement control and the setting of the blood loss area (2d)

This article comes from the notes of learning chutianbo teacher, link to station b

After we have made the map and recovery props, we need to add an enemy to the map.

Enemy's Simple Move 1

public class EmenyController : MonoBehaviour
{
    //设定移动速度变量
    public float speed = 0.1f;
    //声明一个2d刚体对象
     Rigidbody2D rigidbody2D;
    // 声明 Vector2 对象来存放敌人当前位置
    Vector2 position;
    //声明一个初始 y 坐标变量
    float initY;
    //声明一个移动方向的变量
    float direction;
    //存放移动距离的变量,设置为共有,开放在 unity 中的访问
    public float distance = 4;
    void Start()
    {
        // 获取这些对象或变量在游戏开始时的值
        rigidbody2D = GetComponent<Rigidbody2D>();
        //获取起始位置
        position = transform.position;
        //获取起始y坐标
        initY = position.y;
        //设定初始移动方向
        direction = 1.0f;
    }

    // Update is called once per frame

    private void FixedUpdate()
    {
        //通过刚体移动的方法调用,放入 fixupdate方法中,0.02秒执行一次
        MovePosition();
    }
    private void MovePosition()
    {
        if (position.y - initY < distance && direction > 0)
        {
            position.y += speed;
        }
        if (position.y - initY >= distance && direction > 0)
        {
            direction = -1.0f;
        }
        if (position.y - initY > 0 && direction < 0)
        {
            position.y -= speed;
        }
        if (position.y - initY <= 0 && direction < 0)
        {
            direction = 1.0f;
        }
        rigidbody2D.position = position;
     
    }
}

This code opens up the movement speed, and moves back and forth by determining the distance from the initial position (only set on the y-axis); of course, you can change the code on the x-axis

The second type of mobile control (from the official website)

public class EnemyController2 : MonoBehaviour
{
    public float speed;
    public bool vertical;
    Rigidbody2D rigidbodyroot;
    //朝一个方向走的时间
    public float changeTime = 3.0f;
    //计时器
    float timer;
    //方向
    int direction=1;

     void Start()
    {
        rigidbodyroot = GetComponent<Rigidbody2D>();
        timer = changeTime;
    

    }
    private void Update()
    {
     
        timer -= Time.deltaTime;
        if (timer < 0)
        {
            direction = -direction;
            timer = changeTime;
        }
    }
    private void FixedUpdate()
    {
     
        Vector2 position = rigidbodyroot.position;
        if (vertical)
        {
            position.y = position.y + speed * Time.deltaTime*direction;
            
        }
        else
        {
            position.x = position.x + speed * Time.deltaTime * direction;
         
        }
        rigidbodyroot.MovePosition(position);
    }
    //刚体碰撞事件
    private void OnCollisionEnter2D(Collision2D other)
    {
        RubyController rubyController = other.gameObject.GetComponent<RubyController>();
        if (rubyController != null)
        {
            rubyController.changehealth(-1);

        }
    }
   
}

This code opens up the reentry time, speed and walking direction; the walking distance is controlled by the walking time;
at the same time, when the robot touches the protagonist ruby, the protagonist loses blood;

Join the bleeding area

insert image description here
In the blood loss area, we use nails as the image material, because we want to receive the judgment all the time, not only when the trigger is hit, so in the code
we use OnTriggerStay2D to indicate that the trigger will be triggered every frame

        RubyController rubyController = collision.GetComponent<RubyController>();
        if (rubyController != null)
        {

            rubyController.changehealth(DamageNum);

        }

Question 1: When this area needs to be larger, our materials will be reused, here we need to use tiled materials

The first thing to do is to tile the material. We need to go back to the material interface, find the Mesh Type under sprite mode, use Full Rect,
and then check Auto Tiling in the Box Collider 2D in the prefab.
Question 2: The judgment code used
in Unity comes with three kinds of judgments OnTriggerEnter2D OnTriggerStay2D OnTriggerExit2D respectively means once when entering,
once every frame in the trigger and once when leaving;

The material used in this article comes from the unity store Ruby's adventure
link unity official website

Guess you like

Origin blog.csdn.net/jijikok/article/details/125950183