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

发布于:2024-05-04 ⋅ 阅读:(24) ⋅ 点赞:(0)

在这里插入图片描述


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

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

👨‍💻 本文由 秩沅 原创

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

🅰️推荐专栏

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




前言

请添加图片描述

在这里插入图片描述


🎶( 玩家逻辑相关



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; //技能命中攻击力为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#专题篇】—核心章题单实践练习


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


在这里插入图片描述