王者荣耀实时对战服务器Photon之PUN创建角色预设(二)

发表于2017-11-18
评论0 1.3k浏览

本节将指导您如何从头开始创建将并使用Player Prefab,我们将一步一步创建Player Prefab,来完善Photonpun 创建加入房间之后,角色的互动操作。

PUN基本入门之创建角色预设(一)


当您考虑在场景中达到的效果时,使用这几行代码来实现。现在让我们来实现相机的功能,我们需要相机能够围绕我们,所以需要一个相机跟随。

Camera Setup

在本节中,我们将使用CameraWork脚本来完成对“Player”预制整体创建过程。 如果要从头开始写入CameraWork,我们会在下一部分中进行讲解。

将组件CameraWork添加到我的MyRobotKyle机器人预制上。

将物体中心偏移设置为0,4,0,这使得相机看起来更高,让玩家更好地观察环境。

运行场景进行测试,并移动角色,以验证相机是否正确跟随角色。

Beams Setup

我们的机器人角色还没有添加它的武器,让我们从眼睛里创造出一些激光束出来,来代替他的武器。

Adding the Beams models

添加光束模型让光束与眼睛对准。

1.打开测试场景

2.将一个立方体添加到场景中,将其进行命名。

3.修改它的形状让他看起来像一个长梁,并设置正确位置放到左眼。

4.在层次结构中选择我的Kyle Robot实例

5.找到Head节点

6.添加一个空的gameobject作为头GameObject的孩子,命名为Beams。

7.将光束向左拖放到光束组内。

8.重复光束,将其命名为光束。

9.定位它,使其与右眼对齐。

10.从梁对齐中移除盒子对撞机组件

11.调整梁左“盒对撞机”中心和大小封装两个梁

12.将梁左的“箱子对撞机”将“触发”属性设置为“真”,我们只想被告知射击者触摸球员,而不是碰撞。

13.创建一个新的材质,并将其命名为Red Beam,将其保存在“DemoAnimator_tutorial / Scenes /

14.将红光束材料分配给两个光束

15.将预制实现下面图片中的效果,进行一下对比:

Controlling the Beams with User Input

好的,现在我们拥有了武器,让我们按下鼠标左键或者Fire输入来触发它们。

我们来创建一个名为PlayerManager的新C#脚本。 以下是第一个完整版本的代码,以使光束工作。

using UnityEngine;

using UnityEngine.EventSystems;

using System.Collections;

namespace Com.MyCompany.MyGame

    /// <summary>

    /// Player manager. 

    /// Handles fire Input and Beams.

    /// </summary>

    public class PlayerManager : MonoBehaviour 

    {

        #region Public Variables

        [Tooltip("The Beams GameObject to control")]

        public GameObject Beams;

        #endregion

        #region Private Variables

        //True, when the user is firing

        bool IsFiring;

        #endregion

        #region MonoBehaviour CallBacks

        /// <summary>

        /// MonoBehaviour method called on GameObject by Unity during early initialization phase.

        /// </summary>

        void Awake()

        {

            if (Beams == null)

            {

                Debug.LogError("<Color=Red><a>Missing</a></Color> Beams Reference.", this);

            }

            else

            {

                Beams.SetActive(false);

            }

        }

        /// <summary>

        /// MonoBehaviour method called on GameObject by Unity on every frame.

        /// </summary>

        void Update()

        {

            ProcessInputs ();

            // trigger Beams active state 

            if (Beams != null && IsFiring != Beams.GetActive()) 

            {

                Beams.SetActive(IsFiring);

            }

        }

        #endregion

        #region Custom

        /// <summary>

        /// Processes the inputs. Maintain a flag representing when the user is pressing Fire.

        /// </summary>

        void ProcessInputs()

        {

            if (Input.GetButtonDown("Fire1")) 

            {

                if (!IsFiring)

                {

                    IsFiring = true;

                }

            }

            if (Input.GetButtonUp("Fire1")) 

            {

                if (IsFiring)

                {

                    IsFiring = false;

                }

            }

        }

        #endregion

    }

这个脚本在这个阶段的要点是激活或停用光束。当激活时,当与其他模型发生碰撞时,光束将有效地触发,因此我们稍后会捕获这些触发。

1.打开测试场景

2.在场景层次结构中选择我的kyle机器人

3.将PlayerManager组件添加到我的kyle机器人

4.将我的My Kyle Robot/Root/Ribs/Neck/Head/Beams拖放到检查器中的PlayerManager Beams属性中

5.将实例中的更改应用到Prefab

6.如果您运行了场景,并按Fire1输入(默认情况下是鼠标左键或左键),则当您释放时或者松开鼠标时候,光束将显示并立即隐藏。

Health Setup

让我们来晚上PlayerManger脚本,来实现一个相对简单健康的系统,在释放子弹的时候实际上是一个持续的能量流。当这个能量流撞击到我们的时候该如何操作。

打开PlayerManager脚本

PlayerManager转换为Photon.PunBehavour,以公开PhotonView组件

public class PlayerManager : Photon.PunBehaviour {

在公共变量区域内添加公共属性

[Tooltip("The current Health of our player")]

public float Health = 1f;

将以下两种方法添加到MonoBehaviour CallBacks区域。 然后保存Player Manager脚本。

/// <summary>

/// MonoBehaviour method called when the Collider 'other' enters the trigger.

/// Affect Health of the Player if the collider is a beam

/// Note: when jumping and firing at the same, you'll find that the player's own beam intersects with itself

/// One could move the collider further away to prevent this or check if the beam belongs to the player.

/// </summary>

void OnTriggerEnter(Collider other) 

    if (!photonView.isMine)

    {

        return;

    }

    // We are only interested in Beamers

    // we should be using tags but for the sake of distribution, let's simply check by name.

    if (!other.name.Contains("Beam"))

    {

        return;

    } 

    Health -= 0.1f;

/// <summary>

/// MonoBehaviour method called once per frame for every Collider 'other' that is touching the trigger.

/// We're going to affect health while the beams are touching the player

/// </summary>

/// <param name="other">Other.</param>

void OnTriggerStay(Collider other) 

    // we dont' do anything if we are not the local player.

    if (! photonView.isMine) 

    {

        return;

    }

    // We are only interested in Beamers

    // we should be using tags but for the sake of distribution, let's simply check by name.

    if (!other.name.Contains("Beam"))

    {

        return;

    }

    // we slowly affect health when beam is constantly hitting us, so player has to move to prevent death.

    Health -= 0.1f*Time.deltaTime; 

保存这个脚本

首先,两种方法几乎相同,唯一的区别是我们使用DeltaTime在TriggerStay期间减少内存,以减少速度而不依赖于帧速率。这是一个通常适用于动画的重要概念,但是在这里,我们也需要这样做,我们希望通过所有设备以可预测的方式减少内存状况,在更快的计算机上,您的内存状况会下降得更快,Deltatime在这里保证一致性。如果您有任何疑问并通过搜索Unity社区了解DeltaTime,直到您完全吸收了这个概念,那么这是至关重要的。

第二个重要的方面,现在应该明白的是,我们只影响本地玩家的内存,所以我们早点退出的原因是PhotonView不是我的。

最后,我们只想影响内存使用效率,如果我们的对象是一个光束,所以我们使用标签beam”来检查它,这是我们如何标记我们的光束GameObjects

为了方便调试,我们使Health生成一个公共float,这样在我们等待UI被构建的时候很容易检查它的值

Health checking for game over

为了保持简单,当玩家的内存状况达到0时,我们只需离开房间,如果你还记得,我们已经在GameManager脚本中创建了一个方法来离开房间。我们如何创建强大的功能同时减少内存的使用效率。

1.打开GameManager脚本

2.在公共属性区域添加此变量

static public GameManager Instance;

3.在Start()方法中添加新的一段代码

Instance = this;

4.保存这个脚本

注意,我们已经用[static]关键字装饰了Instance变量,这意味着这个变量是可用的,而不必持有一个指向GameManager实例的指针,所以你可以简单地从代码中的任何地方执行GameManager.instance.xxx()。 使用这个单例模式,在游戏中是经常去实用的,让我们来看看如何适合我们的游戏逻辑管理.

1.打开PlayerManager脚本

2.在Update()方法中,在ProcessInputs之后,我们来添加它并保存PlayerManager脚本

 

if ( Health <= 0f)

    GameManager.Instance.LeaveRoom();

3.保存PlayerManager脚本


不管你有关于Photon产品的问题或者是Photon价格问题或者Photon教程方面的问题或者其他问题都可以联系我们给我我们留言,我们真诚的为您服务。关注我们公众号PhotonServer获取等多新鲜资讯。


关注我们公众号PhotonServer 获取最新教程资源。

王者荣耀实时对战服务器Photon之Pun应用系列文章 如果你喜欢请关注我公众号,并推荐给你你的小伙伴,谢谢。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引