花1.png

类吸血鬼实战(4):创建人物手臂、UI创建角色选择

概要:类吸血鬼实战(3):击中反馈、界面UI、物品数据管理

第十二课:创建人物手臂

增加手臂脚本控制方向

using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

public class Hand : MonoBehaviour
{
    public bool isLeft;
    public SpriteRenderer spriter;

    SpriteRenderer player;
    Vector3 rightPos = new Vector3(0.35f, -0.15f, 0);
    Vector3 rightPosReverse = new Vector3(-0.15f, -0.15f, 0);
    Quaternion leftRot = Quaternion.Euler(0, 0, -35);
    Quaternion leftRotReverse = Quaternion.Euler(0, 0, -135);
    void Awake()
    {
        player = GetComponentsInParent<SpriteRenderer>()[1];
    }
    void LateUpdate()
    {
        bool isReverse = player.flipX;
        if (isLeft)
        {
            transform.localRotation = isReverse ? leftRotReverse : leftRot;
            spriter.flipY = isReverse;
            spriter.sortingOrder = isReverse ? 4 : 6;
        }
        else
        {
            transform.localPosition = isReverse ? rightPosReverse : rightPos;
            spriter.flipX = isReverse;
            spriter.sortingOrder = isReverse ? 6 : 4;
        }
    }
}

增加手臂组件

动态显示手臂

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

public class Weapon : MonoBehaviour
{
    /*...*/
    public void Init(ItemData data)
    {
        /*...*/
        Hand hand = player.hands[(int)data.itemType];
        hand.spriter.sprite = data.hand;
        hand.gameObject.SetActive(true);
    }
    /*...*/
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Item", menuName = "ScriptableObject/ItemData")]
public class ItemData : ScriptableObject
{
    /*...*/
    public Sprite hand;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
    /*...*/
    public Hand[] hands;
    void Awake()
    {
        /*...*/
        hands = GetComponentsInChildren<Hand>(true);
    }
    /*...*/
}

第十三课:UI创建3(角色选择)

使用之前的开始游戏按钮改造

人物选择改变动画控制器

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

public class GameManager : MonoBehaviour
{
    [Header("# 玩家信息")]
    public int playerId;
    /*...*/
    public void GameStart(int id)
    {
        playerId = id;
        health = maxHealth;
        player.gameObject.SetActive(true);
        uilevelUp.Select(playerId % 2);
        Resume();
    }
    /*...*/
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
    /*...*/
    public RuntimeAnimatorController[] animCon;
    /*...*/
    void OnEnable()
    {
        animator.runtimeAnimatorController = animCon[GameManager.instance.playerId];
    }
    /*...*/
}

处理每个角色的特质

创建角色特质信息

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

public class Character : MonoBehaviour
{
    public static float Speed
    {
        get
        {
            // 移动速度
            return GameManager.instance.playerId == 0 ? 1.1f : 1f;
        }
    }
    public static float WeaponSpeed
    {
        get
        {
            // 降低攻速
            return GameManager.instance.playerId == 1 ? 1.1f : 1f;
        }
    }
    public static float WeaponRate
    {
        get
        {
            // 提升转速
            return GameManager.instance.playerId == 1 ? 0.9f : 1f;
        }
    }
    public static float Damage
    {
        get
        {
            // 增加伤害
            return GameManager.instance.playerId == 2 ? 1.2f : 1f;
        }
    }
    public static int Count
    {
        get
        {
            // 增加数量
            return GameManager.instance.playerId == 3 ? 1 : 0;
        }
    }
}

增加速度

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
    /*...*/
    void OnEnable()
    {
        speed *= Character.Speed;
        animator.runtimeAnimatorController = animCon[GameManager.instance.playerId];
    }
     /*...*/
}

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

public class Gear : MonoBehaviour
{
    /*...*/
    void SpeedUp()
    {
        float speed = 3 * Character.Speed;
        GameManager.instance.player.speed = speed + speed * rate;
    }
}

调整攻速/转速

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

public class Weapon : MonoBehaviour
{
    /*...*/
    public void Init(ItemData data)
    {
        /*...*/
        switch (id)
        {
            case 0:
                speed = 150 * Character.WeaponSpeed;
                Batch();
                break;
            default:
                speed = 0.5f * Character.WeaponRate;
                break;
        }
        /*...*/
    }
    /*...*/
}

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

public class Gear : MonoBehaviour
{
    /*...*/
    void RateUp()
    {
        /*...*/
        foreach (Weapon weapon in weapons)
        {
            switch (weapon.id)
            {
                case 0:
                    float speed = 150 * Character.WeaponSpeed;
                    weapon.speed = speed + (speed * rate);
                    break;
                default:
                    speed = 0.5f * Character.WeaponRate;
                    weapon.speed = speed * (speed - rate);
                    break;
            }
        }
    }
    /*...*/
}

增加伤害/旋转数量

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

public class Weapon : MonoBehaviour
{
    /*...*/
    public void LevelUp(float damage, int count)
    {
        this.damage = damage * Character.Damage;
        this.count += count;
        if (id == 0)
        {
            Batch();
        }
        player.BroadcastMessage("ApplyGear", SendMessageOptions.DontRequireReceiver);
    }
    public void Init(ItemData data)
    {
        /*...*/

        // Property Set
        id = data.itemId;
        damage = data.baseDamage * Character.Damage;
        count = data.baseCount + Character.Count;
        /*...*/
    }
    /*...*/
}

Created By @Seeyou | 稀有博客