修复拾取武器,保证角色左右手只存在一个武器,拾取新的武器时会进行更换
Weapon:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- namespace RPG.Combat
- {
- /// <summary>
- /// 武器脚本
- /// </summary>
- [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;//武器预制体
- [SerializeField] bool isRightHanded = true;//装备左右手武器,true为右手,false为左手
- [SerializeField] Projectile projectile = null;//箭矢
- [SerializeField] Vector3 projectileOffset = Vector3.zero;//箭矢的偏移
-
- const string weaponName = "Weapon";//声明一个常量武器
-
- /// <summary>
- /// 诞生武器
- /// </summary>
- /// <param name="rightHand">右手武器产生的位置</param>
- /// <param name="leftHand">左手武器产生的位置</param>
- /// <param name="animator">武器对应的动画状态机</param>
- public void Spawn(Transform rightHand,Transform leftHand, Animator animator)
- {
- DestroyOldWeapon(rightHand, leftHand);//产生武器之前先删除之前的旧武器,
-
- if (equippedPrefab != null)//如果不为空,就实例化,防止为空时出现错误
- {
- Transform handTransform = GetTransform(rightHand, leftHand);
-
- GameObject weapon = Instantiate(equippedPrefab, handTransform);//实例化武器
- weapon.name = weaponName;
-
- }
- if (animatorOverride != null) //如果动画状态不为空,就改变为新的动画状态
- {
- animator.runtimeAnimatorController = animatorOverride;//状态机更改为装备武器的状态
- }
- }
-
- /// <summary>
- /// 删除旧的武器
- /// </summary>
- /// <param name="rightHand">右手武器</param>
- /// <param name="leftHand">左手武器</param>
- private void DestroyOldWeapon(Transform rightHand, Transform leftHand)
- {
- Transform oldWeapon = rightHand.Find(weaponName);// 在右手中查找旧武器
- if (oldWeapon == null)
- {
- oldWeapon = leftHand.Find(weaponName);// 如果右手中没有找到旧武器,则在左手中查找
- }
- if (oldWeapon == null) return;// 如果左右手中都没有找到旧武器,则直接返回
-
- oldWeapon.name = "Destroying";
- Destroy(oldWeapon.gameObject);
- }
-
- /// <summary>
- /// 获取武器出现的位置
- /// </summary>
- /// <param name="rightHand">右手</param>
- /// <param name="leftHand">左手</param>
- /// <returns></returns>
- private Transform GetTransform(Transform rightHand, Transform leftHand)
- {
- Transform handTransform;
- if (isRightHanded)
- {
- handTransform = rightHand;//fasle则装备右手武器
- }
- else
- {
- handTransform = leftHand;//装备左手武器
- }
- return handTransform;
- }
-
- public bool HasProjectile()//是否有箭矢
- {
- return projectile != null;//返回箭不为空
- }
-
- //发射箭矢
- public void LacunchProjectile(Transform rightHand,Transform leftHand,Health target)
- {
- //实例化箭矢 Quaternion.identity没有旋转的状态
- Projectile projectileInstance = Instantiate(projectile, GetTransform(rightHand, leftHand).position+ projectileOffset, Quaternion.identity);
- projectileInstance.SetTarget(target,weaponDamage);//设置箭射击的攻击目标,伤害
-
-
- }
-
- public float GetDamage() //获取伤害
- {
- return weaponDamage;
- }
-
- public float GetRange()//获取武器攻击范围
- {
- return weaponRange;
- }
- }
- }
-
复制代码
|