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


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


相关推荐
cyr___3 分钟前
Unity教程(十六)敌人攻击状态的实现
学习·游戏·unity·游戏引擎
nbsaas-boot26 分钟前
架构卡牌游戏:通过互动与挑战学习系统设计的创新玩法
学习·游戏·架构
FairGuard手游加固30 分钟前
游戏如何应对云手机刷量问题
游戏
哆木1 小时前
部署在线GBA游戏,并通过docker安装启动
游戏·html·gba
优梦创客8 小时前
《黑神话悟空》开发框架与战斗系统解析
unity·游戏开发·黑神话悟空·战斗系统·解包
仙魁XAN13 小时前
Unity 设计模式 之 创造型模式-【工厂方法模式】【抽象工厂模式】
unity·设计模式·工厂方法模式·抽象工厂模式
我要吐泡泡了哦15 小时前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎
躺下睡觉~1 天前
Unity-Transform类-父子关系
java·unity·游戏引擎
躺下睡觉~1 天前
Unity-Transform类-缩放和看向
unity·游戏引擎
君莫愁。1 天前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎