【Unity2D:C#Script】为角色添加回血效果

一、创建草莓预制体

1. 在prefabs文件夹新建一个草莓预制体,并添加到场景面板上

2. 为草莓预制体添加Box Collider 2D组件,设置碰撞体积,设置属性IsTrigger为true

3. 在Scripts文件夹新建一个Strawberry脚本,并添加到草莓预制体上

二、编辑Ruby脚本

1. 创建整型变量maxHealthPoint、healthPoint用来存储最大生命值和当前生命值,在Start()方法中初始化为maxHealthPoint

using Cinemachine.Utility;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEditor.Rendering;
using UnityEngine;

public class Ruby : MonoBehaviour {

    public int maxHealthPoint = 100;
    public int healthPoint;
    private int speed = 5;
    // 创建rb2d对象
    private Rigidbody2D rb2d;

    void Start() {
        // 设置游戏的帧率为60帧
        Application.targetFrameRate = 60;
        healthPoint = maxHealthPoint;
        // 用rb2d接收Rigidbody组件
        rb2d = GetComponent<Rigidbody2D>();
    }

    void Update() {
        // 创建float型Horizontal变量和Vertical变量,用来接受键盘输入
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        // 创建一个二维变量position来存储Rigidbody2D组件中的坐标
        Vector2 position = transform.position;

        // 控制Ruby移动
        position.x += horizontal * speed * Time.deltaTime;
        position.y += vertical * speed * Time.deltaTime;

        rb2d.MovePosition(position);
    }

}

2. 新建changeHealthPoint()方法,接收一个整型参数,无返回值;Mathf.Clamp(int value, int min, int max)方法返回value的值,且value的值不会超出[min max];使用Debug语句在控制台输出当前生命值和最大生命值

using Cinemachine.Utility;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEditor.Rendering;
using UnityEngine;

public class Ruby : MonoBehaviour {

    public int maxHealthPoint = 100;
    public int healthPoint;
    private int speed = 5;
    // 创建rb2d对象
    private Rigidbody2D rb2d;

    void Start() {
        // 设置游戏的帧率为60帧
        Application.targetFrameRate = 60;
        healthPoint = maxHealthPoint;
        // 用rb2d接收Rigidbody组件
        rb2d = GetComponent<Rigidbody2D>();
    }

    void Update() {
        // 创建float型Horizontal变量和Vertical变量,用来接受键盘输入
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        // 创建一个二维变量position来存储Rigidbody2D组件中的坐标
        Vector2 position = transform.position;

        // 控制Ruby移动
        position.x += horizontal * speed * Time.deltaTime;
        position.y += vertical * speed * Time.deltaTime;

        rb2d.MovePosition(position);
    }

    public void changeHealthPoint(int value) {
        healthPoint = Mathf.Clamp(healthPoint+value, 0, maxHealthPoint);
        Debug.Log("HP:" + healthPoint + " / " + maxHealthPoint);
    }

}

三、编辑Strawberry脚本

1. 调用OnTriggerEnter2D()方法(参数collision就是与草莓发生碰撞的游戏物体),此方法只会在有其他对象进入触发器时才会执行;

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

public class Strawberry : MonoBehaviour {

    private void OnTriggerEnter2D(Collider2D collision) {

    }
}

2. 创建ruby对象,用来接收Ruby游戏对象的Ruby脚本;调用Ruby脚本的changeHealthPoint()方法,并传递20作为恢复血量的值;调用Destroy()方法删除自身

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

public class Strawberry : MonoBehaviour {

    private void OnTriggerEnter2D(Collider2D collision) {
        Ruby ruby = collision.GetComponent<Ruby>();

            ruby.changeHealthPoint(25);
            Destroy(gameObject);
    }
}

3. 当有除Ruby以外的游戏对象和草莓发生碰撞时,因为其他游戏对象不会挂载Ruby脚本,因此Unity会报错,所以要给这两条语句加一条if判断语句,只有当ruby对象不为空(即挂载Ruby脚本时)才执行这段代码

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

public class Strawberry : MonoBehaviour {

    private void OnTriggerEnter2D(Collider2D collision) {
        Ruby ruby = collision.GetComponent<Ruby>();
        if( ruby != null ) {
            ruby.changeHealthPoint(25);
            Destroy(gameObject);
        }
    }
}

4. 最后,无论Ruby的生命值为何值时,都能吃掉草莓。我们希望只有Ruby在不满血的情况下才能吃掉草莓,所以应该再加一层if条件语句,用来判断Ruby是否满血

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

public class Strawberry : MonoBehaviour {

    private void OnTriggerEnter2D(Collider2D collision) {
        Ruby ruby = collision.GetComponent<Ruby>();
        if(ruby != null) {
            if(ruby.healthPoint < ruby.maxHealthPoint) {
                ruby.changeHealthPoint(25);
                Destroy(gameObject);
            }
        }
    }
}

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值