Unity——2D小游戏笔记整理

【每日一句:清晨和夜晚都请用尽全力去生活】

目录

一、环境搭建

二、人物

三、相机跟随人物移动

四、平铺精灵

五、血条跟随敌人行走

六、脚本逻辑

【玩家行走方法】

【玩家跳跃方法】

【改变玩家血量值方法】

【创建玩家子弹方法】

【主角血量,改变血条遮罩】

【敌人(减血物体)触发检测,调用玩家改变血量值方法ChangeHealth(int amount)】

【敌人来回移动代码】

【改变敌人血量方法EnemyChangeHealth() 引用敌人血量脚本EnemyHealth EnemySetValue(currentHealth / (float)maxHealth);】


一、环境搭建

1.瓦片地图Tilemaps

创建 2DObject——>Tilemap——>Rectangular

        Window——>2D——>Tile Palette

瓦片集 Sprite Mode从Single改为Multiple

             Pixel Per Unit值从100更改为64

2.添加瓦片地图碰撞:Tilemap Collider2D组件

选择的瓦片不再被视为碰撞体:在Inspector找到Collider Type属性,Sprite更改为None

优化瓦片地图碰撞体Composite Collider2D组件,会自动添加Rigidbody2D,在Tilemap Collider2D,启用Used By Composite

在Rigidbody2D,将Rigidbody Body Type属性设为Static

二、人物

动画

Right_Run——>Left_Run

单击Add Property,然后单击Sprite Render旁边的三角形,再单击Flip X旁边的+图标

使用混合树:

【待完善……】

【上下左右移动】

三、相机跟随人物移动

使用Cinemachine包

Window——>PackageManager——>Cinemachine

使用GameObject——>Cinemachine——>Virtual Camera

Cinemachine Virtual Camera组件

调整Orthgraphic Size(摄像机的一半高度内可容纳多少个单位);Follow 跟随主角

四、平铺精灵

1.首先确保游戏对象的缩放在Transform组件1:1:1

2.在SpriteRender组件中将Draw Mode设为Tiled,Tile Mode改为Adaptive

精灵的Inspector面板,Mesh Type改为Full Rect

血条跟随敌人行走

public class FollowBlood : MonoBehaviour

{

    public Transform player;

    public Image hp;

    public  Camera cam;

    void Update()

    {

        //把人物的坐标转化到屏幕坐标

        var playerScreenPos = cam.WorldToScreenPoint(player.position);

        //再把人物坐标Y加一个高度给到人物

        hp.rectTransform.position = new Vector2(playerScreenPos.x, playerScreenPos.y + 35f);

    }

   

}

六、脚本逻辑

【玩家行走方法】

void Movement()

    {

        float horizontal = Input.GetAxisRaw("Horizontal");

        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);

        Vector2 move = new Vector2(horizontal, 0);

        if (horizontal != 0)

        {

            //lookDir.Set(move.x, 0);

            lookDir.Normalize();

            ani.SetBool("IsSpeed",true);

        }

        else  if(horizontal == 0)

        {

            ani.SetBool("IsSpeed", false);

        }

        //Debug.Log(horizontal);

        if (horizontal > 0)

        {

            ani.SetFloat("LookX", 1);

        }

        else if (horizontal < 0)

        {

            ani.SetFloat("LookX", 0f);

        }

      

}

【玩家跳跃方法】

void Jump()

    {

        if (isGround)

        {

            jumpCount = 2;

        }

        if (isJump && isGround)

        {

            rb.velocity = new Vector2(rb.velocity.x, jumpForce);

            //jumpForce--;

            isJump = false;

        }

        else if (isJump && !isGround && jumpCount > 0)

        {

            rb.velocity = new Vector2(rb.velocity.x, jumpForce);

            jumpCount--;

            isJump = false;

        }

【改变玩家血量值方法】

public void ChangeHealth(int amount)

    {

        if (amount < 0)

        {

            if (isInvincible)

                return;

            isInvincible = true;

            invincibleTimer = timerInvincible;

        }

        currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);

        UIHealth.instance.SetValue(currentHealth / (float)maxHealth);

        Debug.Log(currentHealth + "/" + maxHealth);

    }

【创建玩家子弹方法】

void CreatBullet()

    {

        GameObject bul = Instantiate(bullet, rb.position + Vector2.right * 0.5f,Quaternion.identity) as GameObject;

        Projectile proj = bul.GetComponent<Projectile>();

        proj.Launch(300);

    }

—————————————————————————————————————————

【主角血量,改变血条遮罩】

public class UIHealth : MonoBehaviour

{

    public static UIHealth instance

    {

        get;private set;

    }

    public Image mask;

    float originalSize;

    private void Awake()

    {

        instance = this;

    }

    // Start is called before the first frame update

    void Start()

    {

        originalSize = mask.rectTransform.rect.width;

    }

    public void SetValue(float value)

    {

        mask.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, originalSize * value);

    }

}

【敌人(减血物体)触发检测,调用玩家改变血量值方法ChangeHealth(int amount)】

private void OnTriggerStay2D(Collider2D collision)

    {

        PlayerController controller = collision.GetComponent<PlayerController>();

        if (controller != null)

        {

            controller.ChangeHealth(-1);

        }

    }

【敌人来回移动代码

void Update()

    {

        timer -= Time.deltaTime;

        if (timer < 0)

        {

            direction = -direction;

            timer = changeTime;

        }

        if (direction == 1)

        {

            ani.SetBool("isLeft", true);

        }

        else

        {

            ani.SetBool("isLeft", false);

        }

}

private void FixedUpdate()

    {

        Vector2 position = rb.position;

        if (vertical)

        {

            position.y = position.y + Time.deltaTime * speed * direction;

        }

        else

        {

            position.x = position.x + Time.deltaTime * speed * direction;

        }

        rb.MovePosition(position);

    }

改变敌人血量方法EnemyChangeHealth() 引用敌人血量脚本EnemyHealth EnemySetValue(currentHealth / (float)maxHealth);

public void EnemyChangeHealth(int amount)

    {

      

        currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);

        Debug.Log("敌人当前血量+" + currentHealth + amount);

        eh.EnemySetValue(currentHealth / (float)maxHealth);

        Debug.Log(currentHealth + "/" + maxHealth);

    }

猜你喜欢

转载自blog.csdn.net/m0_63330263/article/details/131014786
今日推荐