制作箭矢射出功能
Projectile
- using RPG.Combat;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- // 定义Projectile类
- public class Projectile : MonoBehaviour
- {
-
- [SerializeField] float speed = 1;//箭矢射出的速度
-
- Health target = null;//攻击目标
- float damage = 0;
-
- private void Update()
- {
- if(target == null)//如果攻击目标为空
- {
- return;
- }
-
- transform.LookAt(GetAimLocation());//让箭朝向目标位置
- // 使用Translate方法让箭根据速度和时间增量来移动-- // Vector3.forward表示projectile的前向向量,speed是速度,Time.deltaTime是上一帧到当前帧的时间差
- transform.Translate(Vector3.forward*speed * Time.deltaTime);
- }
- public void SetTarget(Health target,float damage)//设置目标
- {
- this.target = target;
- this.damage = damage;
- }
-
- private Vector3 GetAimLocation()//获取瞄准的目标位置
- {
- CapsuleCollider targetCapsule = target.GetComponent<CapsuleCollider>();//获取目标的碰撞组件
- if(targetCapsule == null)//如果碰撞为空
- {
- return target.transform.position;//返回目标坐标
- }
-
- // 如果有CapsuleCollider组件,则返回目标位置上方CapsuleCollider高度的一半处 -确保projectile瞄准的是目标的上半身或中心位置
- return target.transform.position + Vector3.up * targetCapsule.height /2;
- }
-
- private void OnTriggerEnter(Collider other)
- {
-
- //print("箭的伤害:" + damage);
- Health health = other.GetComponent<Health>();//获取碰撞触发的生命值组件 return
- if (target != null && health != target) return;//如果目标不为空并且生命值不是攻击的目标 return
- if (health == null || health.IsDead()) return;//如果生命值为空 或者角色已经死亡 return
-
-
-
- target.TakeDamage(damage);
- Destroy(gameObject);
- }
- }
复制代码
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;//箭矢
-
- /// <summary>
- /// 诞生武器
- /// </summary>
- /// <param name="rightHand">右手武器产生的位置</param>
- /// <param name="leftHand">左手武器产生的位置</param>
- /// <param name="animator">武器对应的动画状态机</param>
- public void Spawn(Transform rightHand,Transform leftHand, Animator animator)
- {
- if (equippedPrefab != null)//如果不为空,就实例化,防止为空时出现错误
- {
- Transform handTransform = GetTransform(rightHand, leftHand);
-
- Instantiate(equippedPrefab, handTransform);//实例化武器
-
- }
- if (animatorOverride != null) //如果动画状态不为空,就改变为新的动画状态
- {
- animator.runtimeAnimatorController = animatorOverride;//状态机更改为装备武器的状态
- }
- }
-
- /// <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,Quaternion.identity);
- projectileInstance.SetTarget(target);//设置目标
- }
-
- public float GetDamage() //获取伤害
- {
- return weaponDamage;
- }
-
- public float GetRange()//获取武器攻击范围
- {
- return weaponRange;
- }
- }
- }
-
复制代码
Fighter
复制代码
|