Unity 游戏入门 七、 unity 游戏 世界交互-冲突 World Interactions – Collectibles

这一章主要是为Ruby增加健康值

1.为Ruby增加健康状态 Add a Health Stat to Ruby

public class RubyController : MonoBehaviour
{
    public int maxHealth = 5;
    int currentHealth;
    
    Rigidbody2D rigidbody2d;
    float horizontal;
    float vertical;
    
    // Start is called before the first frame update
    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();

        currentHealth = maxHealth;
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");
    }
    
    void FixedUpdate()
    {
        Vector2 position = rigidbody2d.position;
        position.x = position.x + 3.0f* horizontal * Time.deltaTime;
        position.y = position.y + 3.0f * vertical * Time.deltaTime;

        rigidbody2d.MovePosition(position);
    }

    void ChangeHealth(int amount)
    {
        currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
        Debug.Log(currentHealth + "/" + maxHealth);
    }

}

在上面建立了两个新的变量,maxHealth 和currentHealth 。在开始方法中,给了currentHealth 满值。

ChangeHealth方法为修改Ruby的健康值。Clamp方法,保证了健康值在0到maxHealth 之间。最后一行为将健康值显示在控制台上。

2.Create New Variables

3.在开始时设置满状态 Set Full Health at Game Start

4.添加一个函数来修改健康值 Add a Function to Change Health

void ChangeHealth(int amount)
{
   currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
   Debug.Log(currentHealth + "/" + maxHealth);
}

上面的Clamp函数表示currentHealth + amount的值在0到 maxHealth之间。

5.Check Your Changes in Unity Editor

在Ruby Controller脚本中,可以看到maxHealth 已显示在Unity Editor中,因为它是公共变量,可以在外边修改它的值。

6.Exercise: Expose Another Variable

7.什么是触发器What is a Trigger?

触发器是一种特殊类型的碰撞体。它不会阻碍运动。但物理系统仍检查角色是否会与它们发生碰撞。当你的角色进入触发器时,它会向你发送一条消息,这样你就可以处理这个事件了。

8.Create a Collectible Health GameObject

在工程窗口中,Assets > Art > Sprites > VFX,找到CollectibleHealth。在场景中导入它。写上正确的PPU 的值。对这个新的游戏对象增加Box Collider 2D,并调整下大小和位置。

在视图中,找到Box Collider 2D ,将Is Trigger勾选上。

9.增加新的脚本Create a Collectible Script

增加新的脚本名为HealthCollectible,并将这个脚本放到游戏对象中。双击打开这个脚本。

public class HealthCollectible : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
   {
       Debug.Log("Object that entered the trigger : " + other);
   }
}

点运行时,让Ruby走动,看Ruby是否引发触发器。

10.Give Ruby Health

11.Review Your Code

12.Adjust the RubyController Script

13.Check Whether Ruby Needs Health

14.Define a Property in RubyController

私有属性在外部的访问,get ,set的用法。

15.Use the Property in HealthCollectible

16.Check Your Scripts

完整的脚本为:

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

public class HealthCollectible : MonoBehaviour
{
    void OnTriggerEnter2D(Collider2D other)
    {
        RubyController controller = other.GetComponent<RubyController>();

        if (controller != null)
        {
            if(controller.health  < controller.maxHealth)
            {
                controller.ChangeHealth(1);
                Destroy(gameObject);
            }
        }
    }
}
public class RubyController : MonoBehaviour
{
    public float speed = 3.0f;
    
    public int maxHealth = 5;
    
    public int health { get { return currentHealth; }}
    int currentHealth;
    
    Rigidbody2D rigidbody2d;
    float horizontal;
    float vertical;
    
    // Start is called before the first frame update
    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();
        currentHealth = maxHealth;
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");
    }
    
    void FixedUpdate()
    {
        Vector2 position = rigidbody2d.position;
        position.x = position.x + speed * horizontal * Time.deltaTime;
        position.y = position.y + speed * vertical * Time.deltaTime;

        rigidbody2d.MovePosition(position);
    }

    public void ChangeHealth(int amount)
    {
        currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
        Debug.Log(currentHealth + "/" + maxHealth);
    }
}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

computerclass

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值