『功能项目』技能释放【08】
我们打开上一篇07摄像机跟随角色的项目,
本章要做的事情是主角按F按键释放技能,按E键切换技能类型的功能。
首先创建一个空文件夹Resources
创建一个球体Shpere
修改球体缩放尺寸
创建一个材质Material
将材质重命名为Yellow,色板调至为黄色
将Yellow材质球拖拽至球体Shpere对象身上
设置一个新输入系统
拖拽一个攻击动画
连线
创建一个参数
设直线
创建脚本代码:MagicBall.cs
脚本绑定在Resources文件下的Sphere预制体
using UnityEngine;
public class MagicBall : MonoBehaviour{
float speed = 10.0f;
public float lifeTime = 2;
float startTime;
void Start(){
transform.position = new Vector3(transform.position.x,
transform.position.y + 1f, transform.position.z);
startTime = Time.time;
}
void Update(){
transform.position += speed * transform.forward * Time.deltaTime;
if (startTime + lifeTime < Time.time)
Destroy(gameObject);
}
void OnTriggerEnter(Collider other){
//稍后补充碰撞逻辑
}
}
创建脚本:SkillRelease.cs
using UnityEngine;
using System.Collections;
public class SkillRelease : MonoBehaviour{
GameObject prefabBall;
float straightSkillCD = 0.2f;
float sectorSkillCD = 0.5f;
float lastShootTime;
int curSkill { get; set; }
void Awake(){
prefabBall = Resources.Load<GameObject>("Sphere");
}
public void Release(bool keyDown, bool keyPressed){
switch (curSkill){
case 0:
if (keyDown)
StartCoroutine(DelayedPistolFire(0.5f));
break;
case 1:
if (keyDown)
StartCoroutine(DelayedShotgunFire(0.5f));
break;
}
}
public int Change(){
curSkill += 1;
if (curSkill == 3)
curSkill = 0;
return curSkill;
}
void StraightSkill(){
if (lastShootTime + straightSkillCD > Time.time)
return;
lastShootTime = Time.time;
GameObject sphere = Instantiate(prefabBall, null);
sphere.transform.position = transform.position + transform.forward * 2.0f;
sphere.transform.forward = transform.forward;
}
void SectorSkill(){
if (lastShootTime + sectorSkillCD > Time.time)
return;
lastShootTime = Time.time;
for (int i = -2; i <= 2; i++){
GameObject sphere = Instantiate(prefabBall, null);
Vector3 dir = Quaternion.Euler(0, i * 10, 0) * transform.forward;
sphere.transform.position = transform.position + dir * 2.0f;
sphere.transform.forward = dir;
MagicBall ball = sphere.GetComponent<MagicBall>();
ball.lifeTime = 1f;
}
}
IEnumerator DelayedPistolFire(float delay){
yield return new WaitForSeconds(delay);
StraightSkill();
}
IEnumerator DelayedShotgunFire(float delay){
yield return new WaitForSeconds(delay);
SectorSkill();
}
}
将代码绑定在主角常态身上
增加PlayerRayClickNavigation.cs脚本代码
运行项目即可实现按键盘F按键实现释放魔法弹效果,按键盘E键切换技能释放类型,再次按F键变为群体技能。
本篇只实现了摄像机跟随主角功能,接下来还需做以下内容:
1.怪物的生成
2.怪物UI信息(笼)
3.3D模型投射UI(UGUI)界面
4.坐骑在主角身旁召唤召回功能
5.异步传送功能
6.目标点巡航功能
7.动画事件
以及开放回合制、坐骑系统、宠物系统、背包系统、神炼系统、商城系统、Boss的目标跟随任务导航系统以及UI播放3D动画效果等等。
具体项目运行效果请关注water1024的b站视频项目演示《破碎纪元》
【Unity回合2.5D】破碎纪元_单机游戏热门视频 (bilibili.com)https://www.bilibili.com/video/BV1rZY4e9Ebs/?spm_id_from=333.999.0.0