Java教程

Ruby‘s Adventrue游戏制作笔记(八)Unity伤害陷阱

本文主要是介绍Ruby‘s Adventrue游戏制作笔记(八)Unity伤害陷阱,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Ruby's Adventrue游戏制作笔记(八)Unity伤害陷阱

  • 前言
  • 一、添加碰撞体
  • 二、添加脚本
  • 三、修改玩家脚本
  • 四、修改玩家刚体
  • 五、调整伤害区域大小
  • 系列链接


前言

本文章是我学习Unity官方项目项目所做笔记,作为学习Unity的游戏笔记,在最后一章会发出源码,如果等不及可以直接看源码,里面也有很多注释相关,话不多说,让Ruby动起来!
游戏引擎:Unity2020.3

一、添加碰撞体

在这里插入图片描述
调整碰撞范围
选择为Trigger
在这里插入图片描述

二、添加脚本

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


// 伤害陷阱的脚本
public class DamageArea : MonoBehaviour
{
    private int AreaDamage = -1;


    // 待在区域内受到伤害
    private void OnTriggerStay2D(Collider2D collision)
    {
        PlayerController pc = collision.GetComponent<PlayerController>();
        if(pc != null)
        {
            pc.ChangeHealth(AreaDamage);
        }
    }


}


三、修改玩家脚本

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

public class PlayerController : MonoBehaviour
{

    // 玩家速度
    public float speed = 5f;

    // 玩家最大生命值
    private int maxHealth = 5;

    // 当前生命值
    private int currentHealth;

    // 受到伤害后的无敌时间 无敌时间1秒
    private float invincibleTime = 1f; 

    // 无敌计时器
    private float invincibleTimer;

    // 是否无敌
    private bool isInvincible; 

    // 获得最大生命值
    public int myMaxHealth
    {
        get
        {
            return maxHealth;
        }
    }

    // 获得当前生命值
    public int myCurrentHealth
    {
        get
        {
            return currentHealth;
        }
    }

    // 获得玩家刚体
    private Rigidbody2D rbody;

    // Start is called before the first frame update
    void Start()
    {
        currentHealth = 2;
        invincibleTimer = 0;
        // 获得玩家刚体
        rbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
       

        float moveX = Input.GetAxisRaw("Horizontal");  // 控制水平移动方向 按下A时返回 -1,按下D时返回 1
        float moveY = Input.GetAxisRaw("Vertical"); // 控制垂直方向 W:1 S:-1 

        // 创建position坐标,存储玩家输入的坐标
        Vector2 position = rbody.position;
        position.x += moveX * speed * Time.deltaTime;
        position.y += moveY * speed * Time.deltaTime;

        // 将最终坐标赋值给玩家
       // transform.position = position;

        // 使用刚体
        rbody.position = position;

        // 无敌计时
        if (isInvincible)
        {
            invincibleTimer -= Time.deltaTime;
            if (invincibleTimer < 0)
            {
                // 倒计时结束后取消无敌状态
                isInvincible = false;
            }
        }
    }


    // 改变玩家生命值
    public void ChangeHealth(int amount)
    {
        // 如果受到伤害,判断是否无敌
        if(amount < 0)
        {
            // 无敌,不执行
            if (isInvincible == true) return;

            // 不无敌,执行,并且设定为无敌状态
            isInvincible = true;
            invincibleTimer = invincibleTime;

        }

        Debug.Log(currentHealth + "/" + maxHealth);
        // 把玩家的生命值约束在 0 和 最大值 之间
   
        currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
       

        //currentHealth += amount;
        Debug.Log(currentHealth + "/" + maxHealth);
    }
}

四、修改玩家刚体

修改玩家刚体,选择Sleeping Mode 为Never Sleep
在这里插入图片描述

五、调整伤害区域大小

调整伤害区域的Sprite Renderer中的Draw Mode为Tiled

在这里插入图片描述
此时拖动大小则变成

在这里插入图片描述
将原元素的Mesh Type 变为Full Rect

在这里插入图片描述
调整碰撞器范围
添加为预制体

系列链接

还未编辑,发完后发链接。

这篇关于Ruby‘s Adventrue游戏制作笔记(八)Unity伤害陷阱的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!