【UnityRPG游戏制作】Unity_RPG项目_玩家逻辑相关


👨‍💻个人主页@元宇宙-秩沅

👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

👨‍💻 本文由 秩沅 原创

👨‍💻 收录于专栏就业宝典

⭐🅰️推荐专栏⭐

⭐-软件设计师高频考点大全



文章目录

    • ⭐前言⭐
    • [🎶(==一==) 玩家逻辑相关](#🎶(==一==) 玩家逻辑相关)
    • [(==1==) 玩家移动逻辑和动画相关](#(==1==) 玩家移动逻辑和动画相关)
    • [(==2==) 玩家切换](#(==2==) 玩家切换)
    • [(==3==) 人物动画](#(==3==) 人物动画)
    • (==4==)人物技能
    • ⭐🅰️⭐

⭐前言⭐


🎶(一) 玩家逻辑相关



(1) 玩家移动逻辑和动画相关


'

csharp 复制代码
   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);

       }
   }
csharp 复制代码
     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) 玩家切换


csharp 复制代码
    /// <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里的
csharp 复制代码
 //当有武器的时候
 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
csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//-------------------------------
//-------功能:   技能脚本
//-------创建者:         -------
//------------------------------

public class Bullet : MonoBehaviour
{
    public int attack = 20; //技能命中攻击力为20
    EnemyController 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#专题篇】---核心章题单实践练习


你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!


相关推荐
RReality3 小时前
【Unity Shader URP】Matcap 材质捕捉实战教程
java·ui·unity·游戏引擎·图形渲染·材质
魔士于安4 小时前
unity urp材质球大全
游戏·unity·游戏引擎·材质·贴图·模型
Swift社区4 小时前
鸿蒙游戏 UI 怎么设计才不乱?
游戏·ui·harmonyos
南無忘码至尊6 小时前
Unity学习90天 - 第 6 天 -学习物理 Material + 重力与阻力并实现弹跳球和冰面滑动效果
学习·unity·游戏引擎
mxwin9 小时前
Unity 单通道立体渲染(Single Pass Instanced)对 Shader 顶点布局的特殊要求
unity·游戏引擎·shader
Swift社区10 小时前
鸿蒙游戏中的“智能 NPC”架构设计
游戏·华为·harmonyos
王杨游戏养站系统11 小时前
3分钟!玩转游戏下载站系统!蜘蛛池seo功能完善部署!
游戏·游戏下载站养站系统·游戏养站系统
魔士于安11 小时前
unity 低多边形 无人小村 木质建筑 晾衣架 盆子手推车,桌子椅子,罐子,水井
游戏·unity·游戏引擎·贴图·模型
RReality11 小时前
【Unity Shader URP】简易卡通着色(Simple Toon)实战教程
ui·unity·游戏引擎·图形渲染·材质
魔士于安12 小时前
unity 骷髅人 连招 武器 刀光 扭曲空气
游戏·unity·游戏引擎·贴图·模型