👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:就业宝典
⭐🅰️推荐专栏⭐
⭐-软件设计师高频考点大全⭐
文章目录
- ⭐前言⭐
- 🎶(==一==) 玩家逻辑相关
- (==1==) 玩家移动逻辑和动画相关
- (==2==) 玩家切换
- (==3==) 人物动画
- (==4==)人物技能
- ⭐🅰️⭐
⭐前言⭐
🎶(一) 玩家逻辑相关
(1) 玩家移动逻辑和动画相关
‘
private void Move(){float horizontal = Input.GetAxis("Horizontal");float vertical = Input.GetAxis("Vertical");Vector3 dirction=new Vector3(horizontal,0, vertical);if(dirction!=Vector3.zero){transform.rotation=Quaternion.LookRotation(dirction);transform.Translate(Vector3.forward*3*Time.deltaTime);ani.SetBool("run", true);}else{ani.SetBool("run", false);}}
if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0){//方向Vector3 dirction = new Vector3(horizontal, 0, vertical);//让角色的看向与移动方向保持一致transform.rotation = Quaternion.LookRotation(dirction);rigidbody.MovePosition(transform.position + dirction * speed * Time.deltaTime);animator.SetBool("walk", true);//加速奔跑if (Input.GetKey(KeyCode.LeftShift) ){animator.SetBool("run", true);animator.SetBool("walk", true); rigidbody.MovePosition(transform.position + dirction * speed*3 * Time.deltaTime);}else {animator.SetBool("run", false);;animator.SetBool("walk", true);} }else {animator.SetBool("walk", false);}
(2) 玩家切换
/// <summary>/// 更换角色[数组]/// </summary>/// <param name="value"></param>public void ChangePlayers(int value){for (int i = 0; i < playersitem.Length; i++){if (i == value){animator = playersitem[i].GetComponent<Animator>();playersitem[i].SetActive(true);}else{playersitem[i].SetActive(false);}}}/// <summary>/// 键盘监听相关/// </summary>public void InputMonitoring(){if (Input.GetKeyDown(KeyCode.Alpha1)){ChangePlayers(0);}if (Input.GetKeyDown(KeyCode.Alpha2)){ChangePlayers(1);}if (Input.GetKeyDown(KeyCode.Alpha3)){ChangePlayers(2);}}
(3) 人物动画
(4)人物技能
- PlayerContorl里的
//当有武器的时候if (Input.GetKeyDown(KeyCode.Q) && curWeaponNum > 0 ){//实例化技能子弹GameObject bullet = Instantiate(Resources.Load<GameObject>("Perfab/Prop/bullet"), bulletPositon.position , Quaternion.identity);animator.SetBool("skill",true );bullet.GetComponent<Rigidbody>().AddForce(transform.forward * bSpeed);}
- Bullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;//-------------------------------
//-------功能: 技能脚本
//-------创建者: -------
//------------------------------public class Bullet : MonoBehaviour
{public int attack = 20; //技能命中攻击力为20EnemyController enemyController;BossController bossController;void Start(){Destroy(gameObject,10); //十秒销毁}void Update(){}private void OnCollisionEnter(Collision collision){//若碰到敌人,并进行攻击if (collision.gameObject.tag == "enemy"){Debug.Log("技能造成伤害");enemyController = collision.gameObject.GetComponent<EnemyController>();//触发攻击事件enemyController.animator.SetBool("hurt", true); //怪物受伤动画激活enemyController.transform.GetChild(2).GetChild(0).GetComponent<ParticleSystem>().Play();enemyController.hp -= attack; //减少血量enemyController.hpSlider.fillAmount = (enemyController.hp / 100.0f);if (enemyController.hp <= 0 && enemyController.isDied == false) //死亡判断{collision.transform.GetChild(0).GetComponent<Animator>().SetBool("died", true); //死亡动画激活 //播放动画collision.transform.GetChild(2).GetChild(0).GetComponent<ParticleSystem>().Play();enemyController.isDied = true; //确定已经死亡StartCoroutine("delay", collision.gameObject);Destroy(collision.gameObject, 1.3f);}}//若碰到boss//并进行攻击else if (collision.gameObject.tag == "boss"){Debug.Log("造成伤害");bossController = collision.gameObject.GetComponent<BossController>();bossController.hp -= attack;//减少血量Debug.Log(bossController.hp);bossController.hpSlider.fillAmount = (bossController.hp / 300f);if (bossController.hp <= 0) //死亡判断{bossController.isDied = true;collision.transform.GetComponent<Animator>().SetBool("died", true); //死亡动画激活 StartCoroutine("delay", collision.gameObject);Destroy(collision.gameObject, 4);} }}IEnumerator delay(GameObject gameObject) //协程迭代器的定义{//暂停几秒(协程挂起)yield return new WaitForSeconds(1);//暂停两秒后再显示文字 //暴钻石(实例化)Instantiate(Resources.Load<GameObject>("Perfab/Prop/damon"), gameObject.transform.position, Quaternion.identity);}
}
⭐🅰️⭐
⭐【Unityc#专题篇】之c#进阶篇】
⭐【Unityc#专题篇】之c#核心篇】
⭐【Unityc#专题篇】之c#基础篇】
⭐【Unity-c#专题篇】之c#入门篇】
⭐【Unityc#专题篇】—进阶章题单实践练习
⭐【Unityc#专题篇】—基础章题单实践练习
⭐【Unityc#专题篇】—核心章题单实践练习
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!、