武器脚本数据存储,修改战斗脚本,新建武器拾取脚本
Weapon:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- namespace RPG.Combat
- {
- [CreateAssetMenu(fileName = "Weapon", menuName = "Weapons/Make New Waepon", order = 0)]
- public class Weapon : ScriptableObject
- {
- [SerializeField] float weaponDamage = 5f;//武器伤害
- [SerializeField] float weaponRange = 2f;// 武器攻击范围
-
- [SerializeField] AnimatorOverrideController animatorOverride = null;//装备武器动画
- [SerializeField] GameObject equippedPrefab = null;//武器预制体
-
- public void Spawn(Transform handTransform, Animator animator)
- {
- if (equippedPrefab != null)//如果不为空,就实例化,防止为空时出现错误
- {
- Instantiate(equippedPrefab, handTransform);//实例化武器
- }
- if(animatorOverride != null)
- {
- animator.runtimeAnimatorController = animatorOverride;//状态机更改为装备武器的状态
- }
- }
-
- public float GetDamage() //获取伤害
- {
- return weaponDamage;
- }
-
- public float GetRange()//获取武器攻击范围
- {
- return weaponRange;
- }
- }
- }
-
复制代码
Fighter
复制代码 WeaponPickup
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- namespace RPG.Combat
- {
- //
- public class WeaponPickup : MonoBehaviour
- {
- [SerializeField]Weapon weapon = null;
-
-
-
- private void OnTriggerEnter(Collider other)//碰撞触发
- {
- if(other.gameObject.tag == "Player")//如果玩家和拾取武器碰撞触发
- {
- other.GetComponent<Fighter>().EquipWeapon(weapon);//获取玩家的战斗组件,调用准备武器的方法
- Destroy(gameObject);//删除自身
- }
- }
- }
- }
-
复制代码
|