【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#专题篇】---核心章题单实践练习


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


相关推荐
m0_497214153 小时前
unity中通过拖拽,自定义scroll view中子物体顺序
unity·游戏引擎
地狱为王7 小时前
在Unity中实现DTLN-AEC处理音频文件的功能
unity·aec·降噪
颯沓如流星7 小时前
SLG游戏中沙盘元素(通常指资源点、野怪、宝箱等可交互对象)的刷新设计
游戏
m0_552200829 小时前
《UE5_C++多人TPS完整教程》学习笔记49 ——《P50 应用瞄准偏移(Applying Aim Offset)》
c++·游戏·ue5
m0_552200829 小时前
《UE5_C++多人TPS完整教程》学习笔记50 ——《P51 多人游戏中的俯仰角(Pitch in Multiplayer)》
c++·游戏·ue5
SmalBox12 小时前
【URP】Shader绘制棋盘格对比内置管线
unity·渲染
红红大虾13 小时前
前台小姐姐10分钟就能掌握的Defold游戏引擎需要的lua入门知识
游戏
wanhengidc14 小时前
使用云手机能否给企业降本增效
运维·网络·科技·游戏·智能手机
ue星空1 天前
月2期学习笔记
学习·游戏·ue5
wanhengidc1 天前
云手机运行流畅,秒开不卡顿
运维·网络·科技·游戏·智能手机