国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Unity進階–通過PhotonServer實現(xiàn)人物移動和攻擊–PhotonServer(五)

這篇具有很好參考價值的文章主要介紹了Unity進階–通過PhotonServer實現(xiàn)人物移動和攻擊–PhotonServer(五)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Unity進階–通過PhotonServer實現(xiàn)人物移動和攻擊–PhotonServer(五)

DLc: 消息類和通信類

  • Message

    namespace Net
    {
        public class Message
        {
            public byte Type;
            public int Command;
            public object Content;
            public Message() { }
    
            public Message(byte type, int command, object content)
            {
                Type = type;
                Command = command;
                Content = content;
            }
        }
        //消息類型
        public class MessageType
        {
            //unity
            //類型
    
            public static byte Type_UI = 0;
       
            //賬號登錄注冊
            public const byte Type_Account = 1;
            //用戶
            public const byte Type_User = 2;
            //攻擊
            public const byte Type_Battle = 3;
    
            //注冊賬號
            public const int Account_Register = 100;
            public const int Account_Register_Res = 101;
            //登陸
            public const int Account_Login = 102;
            public const int Account_Login_res = 103;
    
            //角色部分
            //選擇角色
            public const int User_Select = 204; 
            public const int User_Select_res = 205; 
            public const int User_Create_Event = 206;
            //刪除角色
            public const int User_Remove_Event = 207;
    
            //攻擊和移動
            //移動point[]
            public const int Battle_Move = 301;
            //移動響應(yīng)id point[]
            public const int Battle_Move_Event = 302;
            //攻擊 targetid
            public const int Battle_Attack = 303;
            //攻擊響應(yīng)id targetid 剩余血量
            public const int Battle_Attack_Event = 304;
    
    
        }
    }
    
    
    • peer類

      using System;
      using System.Collections.Generic;
      using Net;
      using Photon.SocketServer;
      using PhotonHostRuntimeInterfaces;
      using PhotonServerFirst.Bll;
      
      namespace PhotonServerFirst
      {
          public class PSPeer : ClientPeer
          {
              public PSPeer(InitRequest initRequest) : base(initRequest)
              {
      
              }
      
              //處理客戶端斷開的后續(xù)工作
              protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
              {
                  //關(guān)閉管理器
                  BLLManager.Instance.accountBLL.OnDisconnect(this);
                  BLLManager.Instance.userBLL.OnDisconnect(this);
              }
      
              //處理客戶端的請求
              protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
              {
                  PSTest.log.Info("收到客戶端的消息");
                  var dic = operationRequest.Parameters;
      
                  //打包,轉(zhuǎn)為PhotonMessage
                  Message message = new Message();
                  message.Type = (byte)dic[0];
                  message.Command = (int)dic[1];
                  List<object> objs = new List<object>();
                  for (byte i = 2; i < dic.Count; i++)
                  {
                      objs.Add(dic[i]);
                  }
                  message.Content = objs.ToArray();
      
                  //消息分發(fā)
                  switch (message.Type)
                  {
                      case MessageType.Type_Account:
                          //PSTest.log.Info("收到客戶端的登陸消息");
                          BLLManager.Instance.accountBLL.OnOperationRequest(this, message); 
                          break;
                      case MessageType.Type_User:
                          BLLManager.Instance.userBLL.OnOperationRequest(this, message);
                          break;
                      case MessageType.Type_Battle:
                          PSTest.log.Info("收到攻擊移動命令");
                          BLLManager.Instance.battleMoveBLL.OnOperationRequest(this, message);
                          break;
                  }
              }
          }
      }
      
      
  • 客戶端對接類

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using ExitGames.Client.Photon;
    using Net;
    
    public class PhotonManager : MyrSingletonBase<PhotonManager>, IPhotonPeerListener
    {
        private  PhotonPeer peer;
        
        void Awake() {
            base.Awake();
            DontDestroyOnLoad(this);
        }
        // Start is called before the first frame update
        void Start()
        {
            peer = new PhotonPeer(this, ConnectionProtocol.Tcp);
            peer.Connect("127.0.0.1:4530", "PhotonServerFirst");
        }
    
        void Update()
        {
            peer.Service();
        }
    
        private void OnDestroy() {
            base.OnDestroy();
            //斷開連接
            peer.Disconnect();    
        }
    
        public void DebugReturn(DebugLevel level, string message)
        {
    
        }
    
        /// <summary>
        /// 接收服務(wù)器事件
        /// </summary>
        /// <param name="eventData"></param>
        public void OnEvent(EventData eventData)
        {
            
            //拆包
            Message msg = new Message();
            msg.Type = (byte)eventData.Parameters[0];
            msg.Command = (int)eventData. Parameters[1];
            List<object> list = new List<object>();
            for (byte i = 2; i < eventData.Parameters.Count; i++){
                list.Add(eventData.Parameters[i]);
            }
            msg.Content = list.ToArray();
            MessageCenter.SendMessage(msg);
        }
    
        /// <summary>
        /// 接收服務(wù)器響應(yīng)
        /// </summary>
        /// <param name="operationResponse"></param>
        public void OnOperationResponse(OperationResponse operationResponse)
        {
            if (operationResponse.OperationCode == 1){
                Debug.Log(operationResponse.Parameters[1]);
            }
    
        }
    
        /// <summary>
        /// 狀態(tài)改變
        /// </summary>
        /// <param name="statusCode"></param>
        public void OnStatusChanged(StatusCode statusCode)
        {
            Debug.Log(statusCode);
        }
    
        /// <summary>
        /// 發(fā)送消息
        /// </summary>
        public void Send(byte type, int command, params object[] objs)
        {
            Dictionary<byte, object> dic = new Dictionary<byte,object>();
            dic.Add(0,type);
            dic.Add(1,command);
            byte i = 2;
            foreach (object o in objs){
                dic.Add(i++, o);
            }
            peer.OpCustom(0, dic, true);
        }
    
    }
    
    

服務(wù)器

  • BLL管理

    using PhotonServerFirst.Bll.BattleMove;
    using PhotonServerFirst.Bll.User;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace PhotonServerFirst.Bll
    {
        public class BLLManager
        {
            private static BLLManager bLLManager;
            public static BLLManager Instance
            {
                get
                {
                    if(bLLManager == null)
                    {
                        bLLManager = new BLLManager();
                    }
                    return bLLManager;
                }
            }
            //登錄注冊管理
            public IMessageHandler accountBLL;
            //角色管理
            public IMessageHandler userBLL;
            //移動和攻擊管理
            public IMessageHandler battleMoveBLL;
        private BLLManager()
        {
            accountBLL = new PhsotonServerFirst.Bll.Account.AccountBLL();
            userBLL = new UserBLL();
            battleMoveBLL = new BattleMoveBLL();
        }
    
    }
    

    }

  • 移動BLL

    using Net;
    using PhotonServerFirst.Dal;
    using PhotonServerFirst.Model.User;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace PhotonServerFirst.Bll.BattleMove
    {
        class BattleMoveBLL : IMessageHandler
        {
            public void OnDisconnect(PSPeer peer)
            {
                
            }
    
            public void OnOperationRequest(PSPeer peer, Message message)
            {
                object[] objs = (object[])message.Content; 
                switch (message.Command)
                {
                    case MessageType.Battle_Move:
                        PSTest.log.Info("BattleMove收到移動命令");
                        Move(peer, objs);
                        break;
                    case MessageType.Battle_Attack:
                        Attack(peer, objs);
                        break;
                }
    
            }
    
            private void Attack(PSPeer peer, object[] objs)
            {
                //targetid
                //id targetid 剩余血量
                int targetid = (int)objs[0];
                //攻擊者
                UserModel user = DALManager.Instance.userDAL.GetUserModel(peer);
                //被攻擊者
                UserModel targetUser = DALManager.Instance.userDAL.GetUserModel(targetid);
                //計算傷害
                targetUser.Hp -= user.userInfo.Attack;
                foreach (PSPeer otherpeer in DALManager.Instance.userDAL.GetuserPeers())
                {
                    SendMessage.Send(otherpeer, MessageType.Type_Battle, MessageType.Battle_Attack_Event, user.ID, targetUser.ID, targetUser.Hp);
                }
            }
    
                private void Move(PSPeer peer, object[] objs)
                {
                    //位置
                    float[] points = (float[])objs[0];
                    //將要移動的客戶端
                    UserModel user = DALManager.Instance.userDAL.GetUserModel(peer); 
                    user.Points = points;
                    //通知所有客戶端移動
                    foreach (PSPeer otherpeer in DALManager.Instance.userDAL.GetuserPeers())
                    {
                    PSTest.log.Info("通知所有客戶端移動");
                        SendMessage.Send(otherpeer, MessageType.Type_Battle, MessageType.Battle_Move_Event, user.ID, points);
    
                    }
                }
        }
    }
    
    

客戶端

  • 邏輯類

    using System.Collections;
    using System.Collections.Generic;
    using Net;
    using UnityEngine;
    
    public class BattleManager : ManagerBase
    {
        void Start() {
            MessageCenter.Instance.Register(this);
        }
        private UserControll user;
        public UserControll User{
            get{
                if (user == null){
                    user = UserControll.idUserDic[UserControll.ID];
                }
                return user;
            }
        }
    
        void Update()
        {
            if (Input.GetMouseButtonDown(0)){
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                bool res = Physics.Raycast(ray, out hit);
                if (res)
                {
                    if (hit.collider.tag =="Ground"){
                        //Debug.Log("點擊到地面");
                        //移動到hit.point
                        PhotonManager.Instance.Send(MessageType.Type_Battle, MessageType.Battle_Move, new float[] { hit.point.x,hit.point.y, hit.point.z });
                        //Debug.Log(hit.point);
                    }
                    if (hit.collider.tag =="User"){
                        //Debug.Log("點擊到玩家");
                        //獲取距離
                        float dis = Vector3.Distance(hit.collider.transform.position, User.transform.position);
                        if (dis > 0 && dis < 3f){
                            UserControll targetUser = hit.collider.GetComponent<UserControll>();
                            PhotonManager.Instance.Send(MessageType.Type_Battle, MessageType.Battle_Attack, targetUser.id);
                        }
                    }
               }
            }
        }
    
        public override void ReceiveMessage(Message message){
            base.ReceiveMessage(message);
            object[] objs = (object[])message.Content;
            switch (message. Command)
            {
                case MessageType.Battle_Move_Event:
                Debug.Log("移動");
                    Move(objs);
                    break;
                case MessageType.Battle_Attack_Event:
                    Attack(objs);
                    break;
            }
        }
    
    
        //移動
        void Move(object[] objs){
            //移動的用戶id
            int userid = (int)objs[0];
            //移動的位置
            float[] points = (float[])objs[1];
            UserControll.idUserDic[ userid].Move(new Vector3(points[0],points[1],points[2]));
        }
    
        public override byte GetMessageType(){
            return MessageType.Type_Battle;
        }
    
        public void Attack(object[] objs){
            //攻擊者id被攻擊者id 當前剩余血量
            int userid = (int)objs[0];
            int targetid = (int)objs[1];
            int hp = (int)objs[2];
            //攻擊
            UserControll.idUserDic[userid].Attack(targetid);
            if (hp <= 0)
            {
                UserControll.idUserDic[targetid].Die();
            }
        }
    
    }
    
    
  • 角色綁定的類文章來源地址http://www.zghlxwxcb.cn/news/detail-660331.html

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    
    public class UserControll : MonoBehaviour
    {
        //保存了所有角色的集合
        public static Dictionary<int, UserControll> idUserDic = new Dictionary<int, UserControll>();
        //當前客戶端角色的id
        public static int ID;
        private Animator ani;
        //private NavMeshAgent agent;
        //目標位置
        private Vector3 targetPos;
        private bool isRun;
        //角色id
        public int id;
        private bool isDie;
    
        // Start is called before the first frame update
        void Start()
        {
            ani = GetComponent<Animator>();
            //agent = GetComponent<NavMeshAgent>();
        }
    
        // Update is called once per frame
        void Update()
        {
            if(isDie){
                return;
            }
            if (isRun){
                //計算距離
                float dis = Vector3.Distance(transform. position,targetPos);
                if (dis > 0.5f)
                {
                    //移動
                    //agent.isStopped = false;
                   // ani.SetBoo1( "IsRun", true);
                    //agent.SetDestination(targetPos);
                    transform.position = targetPos;
                }
                else
                {
                    //停止
                    //agent.isStopped = true;
                   // ani.setBoo1("IsRun", false);
                    isRun = false;
                }
            }
    
        }
        public void Move(Vector3 target){
            targetPos = target;
            isRun = true;
        }
    
        public void Attack( int targetId){
            //獲得要攻擊的角色
            UserControll targetUser = idUserDic[targetId];
            transform.LookAt(targetUser.transform);
            //ani.SetTrigger( "Attack");
        }
    
        public void Die(){
            //ani. setTrigger("Die");
            isDie = true;
        }
    
    }
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    
    public class UserControll : MonoBehaviour
    {
        //保存了所有角色的集合
        public static Dictionary<int, UserControll> idUserDic = new Dictionary<int, UserControll>();
        //當前客戶端角色的id
        public static int ID;
        private Animator ani;
        //private NavMeshAgent agent;
        //目標位置
        private Vector3 targetPos;
        private bool isRun;
        //角色id
        public int id;
        private bool isDie;
    
        // Start is called before the first frame update
        void Start()
        {
            ani = GetComponent<Animator>();
            //agent = GetComponent<NavMeshAgent>();
        }
    
        // Update is called once per frame
        void Update()
        {
            if(isDie){
                return;
            }
            if (isRun){
                //計算距離
                float dis = Vector3.Distance(transform. position,targetPos);
                if (dis > 0.5f)
                {
                    //移動
                    //agent.isStopped = false;
                   // ani.SetBoo1( "IsRun", true);
                    //agent.SetDestination(targetPos);
                    transform.position = targetPos;
                }
                else
                {
                    //停止
                    //agent.isStopped = true;
                   // ani.setBoo1("IsRun", false);
                    isRun = false;
                }
            }
    
        }
        public void Move(Vector3 target){
            targetPos = target;
            isRun = true;
        }
    
        public void Attack( int targetId){
            //獲得要攻擊的角色
            UserControll targetUser = idUserDic[targetId];
            transform.LookAt(targetUser.transform);
            //ani.SetTrigger( "Attack");
        }
    
        public void Die(){
            //ani. setTrigger("Die");
            isDie = true;
        }
    
    }
    
    

到了這里,關(guān)于Unity進階–通過PhotonServer實現(xiàn)人物移動和攻擊–PhotonServer(五)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • unity進階學習筆記:photonServer測試

    unity進階學習筆記:photonServer測試

    photonServer是由photon發(fā)布的一個網(wǎng)絡(luò)框架,其封裝了UDP和TCP通信機制讓用戶可以直接調(diào)用API實現(xiàn)網(wǎng)絡(luò)游戲通信 1 photonServer下載安裝 進入Photon官網(wǎng)的SDK選項,選擇下載Server。目前Server版本已經(jīng)更新到v5,這里我為了和教程保持一致下載的是老版本v4.下載完后按照安裝指引安裝即可

    2024年02月04日
    瀏覽(14)
  • Unity實現(xiàn)人物旋轉(zhuǎn)+移動

    思路:首先要有個變量去記錄下操作前的一個方向狀態(tài)。(本次操作的對象是正面對著屏幕的。)然后還有有個變量去描述將要發(fā)生的方向。接著要明確,前和后,左和右是橫跨180°的,其他的兩兩是相差90°的。所以我們可以以90°一個單位去做旋轉(zhuǎn)。并且利用前面總結(jié)的方向

    2024年02月14日
    瀏覽(21)
  • Unity實現(xiàn)人物移動、旋轉(zhuǎn)、跳躍

    Unity實現(xiàn)人物移動、旋轉(zhuǎn)、跳躍

    1.Player腳本控制人物移動,可單獨使用。(人物需添加組件 Box ? Collider和Rigidbody ) 2.相機放在人物頭部,轉(zhuǎn)動需要帶著人物轉(zhuǎn),相機轉(zhuǎn)動靈敏度和上下轉(zhuǎn)動角度范圍根據(jù)具體情況配置。 腳本CameraController和Player直接掛載到人物就可以用了。 3. 文件目錄(人物final bowser fly,相

    2024年02月04日
    瀏覽(22)
  • 實現(xiàn)3D人物的移動和旋轉(zhuǎn)。(Unity)

    實現(xiàn)3D人物的移動和旋轉(zhuǎn)。(Unity)

    首先,需要在人物身上加剛體和碰撞器。 ? 如果需要人物身上有聲音,可以添加AudioSource音頻源。 ?然后創(chuàng)建腳本,需要把腳本掛載到對應(yīng)的對象身上。 如果有動畫,還需要創(chuàng)建狀態(tài)機添加到對應(yīng)的對象上面,并且設(shè)置好里面的動畫。 ?代碼實現(xiàn): 圖片實現(xiàn): ? ? 上面代碼

    2024年02月04日
    瀏覽(22)
  • Unity CharacterController控制人物移動(包括重力實現(xiàn))

    提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔 在使用CharacterController組件時,人物移動一般有兩種方式,一種是無重力移動–SimpleMove,一種是有重力移動–Move。而使用有重力移動時,又會出現(xiàn)人在下樓梯時無法貼合地面,從而造成飛天效果,最終導

    2024年02月11日
    瀏覽(18)
  • Unity教程2:保姆級教程.幾行代碼實現(xiàn)輸入控制2D人物的移動

    Unity教程2:保姆級教程.幾行代碼實現(xiàn)輸入控制2D人物的移動

    目錄 人物的創(chuàng)建以及剛體的設(shè)置 圖層渲染層級設(shè)置 角色碰撞箱設(shè)置 使用代碼控制人物移動 創(chuàng)建腳本文件 ?初始函數(shù)解釋 控制移動代碼 初始化變量 ?獲得鍵盤輸入 ?調(diào)用函數(shù) 手冊鏈接在這:Unity User Manual (2019.3) - Unity 手冊 沒有控制人物移動的2D游戲就太說不過去了!那么接

    2024年02月06日
    瀏覽(17)
  • 用Unity3D制作FPS游戲的學習筆記————人物移動、利用鼠標實現(xiàn)視角轉(zhuǎn)動和人物跳躍(含人物懸空不掉落修復(fù))

    用Unity3D制作FPS游戲的學習筆記————人物移動、利用鼠標實現(xiàn)視角轉(zhuǎn)動和人物跳躍(含人物懸空不掉落修復(fù))

    前言: 這是我第一次發(fā)布文章,此文章僅供參考,我也是剛學習接觸untiy,在制作項目的過程中將有用的寫下來記一記,以便自己之后能回頭看看,各位大佬輕點噴,若有錯誤請麻煩積極提謝謝各位。該文章參考自B站UP主蔡先森_rm-rf發(fā)布的 【第一人稱射擊游戲教程2.0【已完結(jié)

    2024年04月27日
    瀏覽(24)
  • 【unity造車輪】3種實現(xiàn)虛擬移動搖桿控制人物移動的方法(實操加詳細講解,全網(wǎng)最全最易理解)

    【unity造車輪】3種實現(xiàn)虛擬移動搖桿控制人物移動的方法(實操加詳細講解,全網(wǎng)最全最易理解)

    素材 繼承ScrollRect,自己手戳代碼,我愿意稱之為最簡單的實現(xiàn)

    2024年02月14日
    瀏覽(16)
  • Unity 原神人物移動和鏡頭處理

    Unity 原神人物移動和鏡頭處理

    每幀都處理的地方 不要用 SetTrigger 為什么呢? 你肯定會希望 SetTrigger run 就跑步 SetTrigger stop 就停止 但事實并非如此 SetTrigger 會在下一幀自動設(shè)置回去 而你移動肯定是每幀都在 SetTrigger 所以人物移動會抽搐 最好的辦法是 設(shè)置float 分析原神的鏡頭 界面左側(cè)負責控制人物移動

    2024年02月07日
    瀏覽(18)
  • Unity Animator人物模型動畫移動偏移

    Unity Animator人物模型動畫移動偏移

    模型動畫出現(xiàn)移動方向偏移 !修改Animation中的Root Transform Rotation(根變換位置)、Root Transform Rotation(x,y,z)(旋轉(zhuǎn)),Bake Info Pose修改為Original??梢越鉀Q !!但是,使用動畫移動函數(shù)時將無法移動,原因是鎖定根變換位置和循環(huán)位置 ?。?!所以只要修改依據(jù)為原始或者微調(diào)偏離值,

    2024年02月15日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包