美文网首页Unity游戏开发学习记录
Unity - Creator Kit: Cactus RPG

Unity - Creator Kit: Cactus RPG

作者: 我阿郑 | 来源:发表于2024-01-01 18:48 被阅读0次

    https://learn.unity.com/project/creator-kit-beginner-code?uv=2020.3

    导入资源 Creator Kit: Beginner Code assets

    双击 ExampleScene 打开场景

    image.png

    点击Play按钮运行:

    image.png

    This is a hack-and-slash RPG game with a point-and-click control system.
    这是一款带有点击式控制系统的砍杀RPG游戏。

    This means you can click on:

    • A location to move there
    • A target to attack
    • An item to add it to your inventory

    Try attacking the fiendish Training Dummy to get started. You can zoom in and out using your scroll wheel.
    尝试攻击恶魔般的训练假人以开始。您可以使用滚轮放大和缩小。

    - hack-and-slash 砍杀
    - point-and-click: n. 指向-点击
    - add it to your inventory `/ˈɪnvəntɔːri/` 添加到您的库存中
    - inventory 库存
    

    ✅ Inventory and character stats

    Click on the inventory satchel in the bottom left-hand corner of the screen to: 点击屏幕左下角的【库存背包】

    image.png
    • Review character stats (strength, defense and agility) 查看角色属性(力量、防御和敏捷)
    • Review equipment and inventory items (hover the mouse over them to reveal more details) 查看装备和物品库存(将鼠标悬停在上面以查看更多详细信息)
    • Use or equip items by double-clicking the icon 通过双击图标使用或装备物品
    image.png

    You can also 【open/close】 the inventory with the keyboard shortcut I.

    ✅ Open the SpawnerSample script file

    When you tested the game in the previous tutorial, you may have noticed that three health potions appear around the PotionSpawner GameObject at game start.

    -  potions: n. 药剂,药水;魔药(potion的复数形式)
    -  PotionSpawner 药水孵化器,可以产生药水
    
    • select the LevelDesign > PotionSpawner GameObject
    • find the Spawner Sample (Script) component.
    image.png

    This component is the script which makes the three potions appear.

    Double-click the SpawnerSample script. This is what you will see in the code editor:

    using UnityEngine;
    using CreatorKitCode;
    
    public class SpawnerSample : MonoBehaviour
    {
        public GameObject ObjectToSpawn;
    
        void Start()
        {
            int angle = 15;
            Vector3 spawnPosition = transform.position;
    
            Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
            spawnPosition = transform.position + direction * 2;
            Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
    
            angle = 55;
            direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
            spawnPosition = transform.position + direction * 2;
            Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
    
            angle = 95;
            direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
            spawnPosition = transform.position + direction * 2;
            Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
        }
    }
    

    ✅ Write your own spawner class

    using CreatorKitCode;
    
    public class LootAngle
    {
        int angle;
        int step;
        public LootAngle(int increment)
        {
            step = increment;
            angle = 0;
        }
    
        public int NextAngle()
        {
            int currentAngle = angle;
            angle = Helpers.WrapAngle(angle + step);
    
            return currentAngle;
        }
    }
    

    修改 SpawnerSample:

    public class SpawnerSample : MonoBehaviour
    {
        public GameObject ObjectToSpawn;
    
        void Start()
        {
            LootAngle myLootAngle = new LootAngle(45);
            //every call will advance the angle!
            SpawnPotion(myLootAngle.NextAngle());
            SpawnPotion(myLootAngle.NextAngle());
            SpawnPotion(myLootAngle.NextAngle());
            SpawnPotion(myLootAngle.NextAngle());
        }
    
        void SpawnPotion(int angle)
        {
            int radius = 5;
    
            Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
            Vector3 spawnPosition = transform.position + direction * radius;
            Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
        }
    }
    

    ✅ Create a new spawning object

    Let’s create a different GameObject which spawns money at game start:
    让我们创建一个不同的游戏对象,它在游戏开始时【生成金钱】:

    In the Hierarchy, right-click and select 3D Object > Sphere from the contextual menu. Rename your new GameObject MoneySpawner.

    In the Scene view, use the Move Tool (shortcut: W) to move the MoneySpawner GameObject next to the PotionSpawner GameObject.

    image.png

    接着,给 MoneySpawner 添加一个已有的名为 Spawner Sample 的脚本:

    image.png

    In the Project window, go to Assets/Creator Kit - Beginner Code/Prefabs/Tutorial and find the MoneyLoot Prefab.

    image.png

    拖拽给 Spawner Sample 的脚本的 Object To Spawn 字段赋值:

    image.png

    ✅ Customize the health potions

    In this tutorial, you’ll write an effect for those potions so they give the player character an additional 10 health points when used.

    image.png

    为“药水”编写代码,玩家使用药水后增加“10 点生命值”。

    In the top menu, go to Beginner Code > Create Item Effect .

    image.png

    Rename to “AddHealthEffect

    image.png

    生成新的脚本:


    image.png

    go to Assets/Creator Kit - Beginner Code/Prefabs /ItemDatabase. Select the Potion Prefab.

    image.png

    In the Inspector, use the Add New Effect drop-down menu to select the AddHealthEffect. This will appear in the Script field beneath.

    image.png
    • In the Description field, add a description of the effect (for example, “Gives 10 HP”).

    ✅ Write the effect function

    修改AddHealthEffect脚本如下:

    using CreatorKitCode;
    
    public class AddHealthEffect : UsableItem.UsageEffect
    {
        public int HealthAmount;
    
        public override bool Use(CharacterData user)
        {
            user.Stats.ChangeHealth(HealthAmount);
            return true;
        }
    }
    

    保存一下,然后就可以看到多了一个 Health Amount 字段,我们设置为10:

    image.png

    保存场景,运行。

    ✅ Customize your game

    This tutorial is a reference guide for further customization of the Creator Kit: Beginner Code game.

    It provides guidance to help you use script templates to create :

    • Usable items with Usage Effects (like the health potion in the final tutorial)
    • Equipment items with Equipped Effects
    • Weapons with Weapon Attack Effects

    使用脚本模版创建:

    • 具有使用效果的可用物品(如最终教程中的健康药水)
    • 具有装备效果的装备物品
    • 具有武器攻击效果的武器

    ➡️ Guide structure

    This guide is a tool to help you consolidate your understanding and experiment with the game, rather than a step-by-step guide.
    It might also be a useful environment to apply what you have learnt in other Unity tutorials!

    This guide has the following structure:

    1. How to create an item type in Unity Editor 如何在Unity Editor中创建item类型
    2. How to create the custom effect script for that item, and basic guidance on using it 如何为该项目创建自定义效果脚本,以及使用它的基本指导
    3. How to apply the effect to the item 如何【将效果应用于item

    ✅ Create a Usable Item

    What is a Usable Item?

    A Usable Item is an item that the player can use by double-clicking on it in their inventory (for example, a health potion).
    可用物品是玩家可以通过在【库存背包中】双击它来使用的物品(例如,健康药水)。

    How do I create a Usable Item in Unity Editor?

    • go to Assets/Creator Kit - Beginner Code/Prefabs/InGameItem:
    image.png
    • 然后右键,选择 Create > Beginner Code > Usable Item,可以给你自定义的Item起个好记的名字
    image.png

    In the Inspector, you will see the following fields (exposed variables):

    image.png
    • 【Item Sprite】: The Sprite that will be used in the inventory for the item. It will also be used to display the object in the world if no World Object Prefab is set (see below). 将在物品库存中使用的精灵。如果未设置世界对象预制件,它也将用于在世界中显示对象(见下文)。

    • 【Item Name】: The name of the item that will appear above it in the game, and in the tooltip in the player’s inventory. 将在游戏中显示在其上方的物品名称,以及玩家物品栏的工具提示中。

    • 【Description】: The description of the item that will be displayed in the tooltip in the player’s inventory. 将在玩家物品栏的工具提示中显示的物品描述。

    • 【World Object Prefab】: The 3D GameObject that will be created to show the Usable Item in the game world when it is spawned. If this is left empty (null), the item Sprite will be used instead. 将创建的 3D 游戏对象,用于在生成可用物品时在游戏世界中显示该物品。如果将其留空 (null),则将改用 Sprite 项。

    • 【Add New Effect】: A drop-down menu listing the Usage Effects you have written. When you write a new effect, it will appear here. Select a Usage Effect to add it to your Usable Item. 一个下拉菜单,列出您编写的 Usage Effects。当你写一个新effect时,它会出现在这里。选择一个Usage Effects以将其添加到您的Usable Item 上。

    In the next step, you’ll learn how to write a Usage Effect script and apply it to your Usable Item.

    下一步学习【如何编写使用Usage Effect 脚本】并将其应用于你自定义的Usable Item 上。

    ✅ Write a Usage Effect script

    What is a Usage Effect script?

    A Usage Effect script specifies what happens when a Usable Item is double-clicked in the player’s inventory. It is added to a Usable Item Asset in the Editor.

    Usage Effect脚本用于指定玩家在物品栏中双击Usable Item时会发生什么。

    How do I create a new Usage Effect script?

    • In the top menu, go to Beginner Code > Create Item Effect.
    image.png

    点击create :

    image.png

    This creates the script in Assets/Scripts/ItemEffect and selects it in the Editor.

    image.png

    生成的文件代码如下:

    using CreatorKitCode;
    
    public class MyItemEffect : UsableItem.UsageEffect
    {
        public override bool Use(CharacterData user)
        {
            return false;
        }
    }
    

    How do I customize the Usage Effect? 如何自定义Usage Effect?

    To customize the Usage Effect, you need to add instructions to the Use function:

    public override bool Use(CharacterData user)
    {
            return false;
    }
    
    • 参数 CharacterData user 代表可以使用物品的 玩家角色
    • 该游戏中只有一个玩家角色(驼鹿)可以使用物品
    • 你可以使用user这个参数访问角色的统计数据、物品库存和装备物品,并对它们进行修改。

    How do I find out more about the custom CharacterData class?
    如何找到有关自定义 CharacterData 类的更多信息?

    image.png

    点击 Open Documentation ,打开如下页面:

    image.png
    • select API Documentation.
    • In the filter bar, search for “CharacterData”.
    image.png

    ➡️ How do I set the return value for the Usable Item?

    The Use function returns a Boolean value (true or false). Set it to:

    • True if the object is used and removed from the player’s inventory (if it's a stack of objects, the count will be reduced by one). 如果物品被使用并从玩家的物品库存中移除(如果它是一组物品,数量会减少一个)。

    • False if the object can’t be used (for example, you can test if health is already at maximum before adding health, and return false if it is so that a health potion won’t be wasted). 如果物品无法使用(例如,在添加血量之前可以测试是否已经达到最大值,如果是则返回false,以避免浪费血药)。

    ✅ Apply the Usage Effect script to the Usable Item

    In the Project window, select the Usable Item you have created :
    选择你创建的Usable Item

    image.png

    find the Add New Effect field. Select your Usage Effect from the drop-down menu.

    image.png
    • Add a Description for the Usage Effect.
    • save your changes.

    ✅ Create an Equipment Item

    An Equipment Item is an item that gives the player character particular attributes when equipped. (Weapons are a special kind of Equipment Item with additional functionality; this is explored separately later in this guide.)

    【装备物品】是一种在装备时赋予玩家角色特定属性的物品。(武器是一种特殊类型的装备物品,具有附加功能;本指南稍后将单独探讨。

    ➡️ How do I create an Equipment Item in Unity Editor?

    Assets/Creator Kit - Beginner Code/Prefabs/InGameItem 路径下, 右键 Create | Beginner Code | Equipment Item,可以重命名一个好记的名字

    image.png

    In the Inspector, you will see the following fields (exposed variables):

    image.png

    大多数字段和创建UsableItem是同理的,有几个特有的如下:

    • 【Slot】: The Equipment Slot on which the Equipment Item can be worn (for example, Head).

    有几个可选项:

    image.png
    Equipment Slot : 装备位置
    `worn`: v. 穿;佩戴;磨损(`wear`的过去分词)
    `Torso`:n. 躯干;裸体躯干雕像;未完成的作品;残缺不全的东西
    `Accessory`:n. 附件, 配件
    
    • 【Minimum Stats】: The minimum Strength, Agility, and Defense status values required to equip the item. If the player doesn’t meet the minimum values for these stats, double-clicking the item in their inventory won’t do anything. 必须给准备设置这些最小值,不然这些装备不起任何作用

    • 【Add New Effect】: Select an Equipped Effect to add it to your Equipment Item.

    ✅ Write an Equipped Effect script

    ➡️ What is an Equipped Effect script?

    An Equipped Effect script enables a custom effect when the player equips an item. It is added to an Equipment Item or Weapon in the Editor.

    ➡️ How do I create a new Equipped Effect script?

    go to Beginner Code > Create Equipped Effect.

    image.png

    This creates the script in Assets/Scripts/EquippedEffect and selects it in the Editor.

    image.png

    生产的脚本代码如下:

    using CreatorKitCode;
    
    public class MyEquippedEffect : EquipmentItem.EquippedEffect
    {
         public override void Equipped(CharacterData user)
         {
         
         }
         
         public override void Removed(CharacterData user)
         {
         
         }
    }
    
    1. void Equipped(CharacterData user) 方法

    This function is called when the player (user) equips the item. Use the function to add the effect — for example, to give the character a +1 Strength stat modifier.
    当玩家(用户)装备物品时,将调用此函数。使用该函数添加效果 - 例如,为角色提供 Strength属性+1 的修改。

    TIP: You can use a private variable to store your chosen modifier.

    1. void Removed(CharacterData user) 方法

    This function is called when the player (user) removes the item. Use this function to remove the effect on the player character.

    ✅ Apply the Equipped Effect script to an Equipment Item

    Assets/Creator Kit - Beginner Code/Prefabs/InGameItem 路径下选择你创建的 Equipment Item ,点击 Add new Effect 就会添加1个 Effect:

    image.png

    ✅ Create a Weapon

    ➡️ What is a Weapon?

    A Weapon is a special Equipment Item that you can equip on the character Weapon Slot.

    In addition to the features of an ordinary Equipment Item, you can also give a Weapon: 除了【普通装备物品的特性】外,您还可以赋予一个武器:

    • Stats relating to attack actions: Reach, Speed, Minimum Damage and Maximum Damage. 与攻击行为相关的属性:攻击范围、攻击速度、最小伤害和最大伤害。

    • A Weapon Attack Effect, which enables you to customize what happens when the weapon damages an enemy character. 武器的攻击效果,可让您自定义武器对敌方角色造成伤害时会发生什么。

    ➡️ How do I create a Weapon in Unity Editor?

    和创建 Equipment Item 步骤一样。

    image.png

    Weapon Stats:

    image.png
    • 【Speed】: How fast the Weapon attacks. This is the cooldown time between two attacks — the lower the value, the faster the next attack. If the value is 0, the next attack will be performed as soon as the first animation is finished.

    武器攻击的速度。这是两次攻击之间的冷却时间,数值越低,下一次攻击越快。如果数值为0,在第一次动画完成后立即执行下一次攻击。

    • 【Minimum Damage】: The minimum amount of damage the Weapon can do, before adding any character modifiers. Damage Done is a random value between the Minimum and Maximum Damage.

    • 【Maximum Damage】: 同理

    • 【Range】: The distance at which the Weapon can damage an enemy character. If the target character is past the Range when the attack should connect, it won't do any damage.

    ✅ Write a Weapon Attack Effect script

    ➡️ What is a Weapon Attack Effect script?

    A Weapon Attack Effect script enables a customized effect when the Weapon hits a target. It is added to a Weapon.

    image.png image.png

    We’ve provided a template script to help you write a Weapon Attack Effect for items:

    using CreatorKitCode;
    
    public class MyWeaponEffect : Weapon.WeaponAttackEffect
    {
        public override void OnAttack(CharacterData target, CharacterData user, ref Weapon.AttackData attackData)
        {
            
        }
        
        public override void OnPostAttack(CharacterData target, CharacterData user, Weapon.AttackData data)
        {
            
        }
    }
    
    • OnAttack 函数

    This function is for instructions which control what happens when the Weapon hits an enemy. You can add damage or apply elemental effects to the target using the 【AddDamage function in attackData】.
    这个函数用于控制武器击中敌人时发生的事件。您可以使用attackData中的AddDamage函数向目标造成伤害或施加元素效果。

    • OnPostAttack 函数

    This function is called after all the OnAttack function instructions are executed. This enables you to customize things that happen in reaction to the total amount of damage the target receives after all OnAttack effects are applied.

    在执行完所有OnAttack函数的指令后,将调用此函数。这使您能够根据target在应用所有OnAttack效果后所受到的【总伤害】定制响应的事件。

    ➡️ How do I find out more about the custom Weapon.AttackData class?

    同理,在 API Documentation 搜 Weapon.AttackData:

    image.png

    ✅ Apply the Weapon Attack Effect script to a Weapon

    image.png

    save your changes.

    TIP: If you want to remove the Weapon Attack Effect, select the minus button (-) to the right of the Script field in the Inspector.

    image.png

    相关文章

      网友评论

        本文标题:Unity - Creator Kit: Cactus RPG

        本文链接:https://www.haomeiwen.com/subject/qcmrndtx.html