Photon Unity Networking基础教程 6 -

作者: 浪尖儿 | 来源:发表于2016-11-04 18:30 被阅读283次

    这部分将会知道你创建CameraWork脚本,再你玩游戏的时候让相机跟随你的角色。这个脚本和网络没什么关系,所以简短介绍。

    主要内容

    • 创建CameraWork脚本

    创建CameraWork脚本

    1. 新建一个C#脚本,命名为CameraWork

    2. 把CameraWork的内容替换成下面的代码

       using UnityEngine;
       using System.Collections;
       
       namespace Com.MyCompany.MyGame
       {
           /// <summary>
           /// Camera work. Follow a target
           /// </summary>
           public class CameraWork : MonoBehaviour
           {
               #region Public Properties
       
               [Tooltip("The distance in the local x-z plane to the target")]
               public float distance = 7.0f;
       
               [Tooltip("The height we want the camera to be above the target")]
               public float height = 3.0f;
       
               [Tooltip("The Smooth time lag for the height of the camera.")]
               public float heightSmoothLag = 0.3f;
       
               [Tooltip("Allow the camera to be offseted vertically from the target, for example giving more view of the sceneray and less ground.")]
               public Vector3 centerOffset = Vector3.zero;
       
               [Tooltip("Set this as false if a component of a prefab being instanciated by Photon Network, and manually call OnStartFollowing() when and if needed.")]
               public bool followOnStart = false;
       
               #endregion
       
               #region Private Properties
               // cached transform of the target
               Transform cameraTransform;
       
               // maintain a flag internally to reconnect if target is lost or camera is switched
               bool isFollowing;
               // Represents the current velocity, this value is modified by SmoothDamp() every time you call it.
               private float heightVelocity = 0.0f;
               // Represents the position we are trying to reach using SmoothDamp()
               private float targetHeight = 100000.0f;
               #endregion
       
               #region MonoBehaviour Messages
               /// <summary>
               /// MonoBehaviour method called on GameObject by Unity during initialization phase
               /// </summary>
               void Start()
               {
                   // Start following the target if wanted.
                   if (followOnStart)
                   {
                       OnStartFollowing();
                   }
               }
       
               /// <summary>
               /// MonoBehaviour method called after all Update functions have been called. This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
               /// </summary>
               void LateUpdate()
               {
                   // The transform target may not destroy on level load, 
                   // so we need to cover corner cases where the Main Camera is different everytime we load a new scene, and reconnect when that happens
                   if (cameraTransform == null && isFollowing)
                   {
                       OnStartFollowing();
                   }
       
                   // only follow is explicitly declared
                   if (isFollowing)
                   {
                       Apply();
                   }
               }
               #endregion
       
               #region Public Methods
               /// <summary>
               /// Raises the start following event. 
               /// Use this when you don't know at the time of editing what to follow, typically instances managed by the photon network.
               /// </summary>
               public void OnStartFollowing()
               {
                   cameraTransform = Camera.main.transform;
                   isFollowing = true;
                   // we don't smooth anything, we go straight to the right camera shot
                   Cut();
               }
               #endregion
               
               #region Private Methods
               /// <summary>
               /// Follow the target smoothly
               /// </summary>
               void Apply()
               {
                   Vector3 targetCenter = transform.position + centerOffset;
       
                   // Calculate the current & target rotation angles
                   float originalTargetAngle = transform.eulerAngles.y;
                   float currentAngle = cameraTransform.eulerAngles.y;
       
                   // Adjust real target angle when camera is locked
                   float targetAngle = originalTargetAngle;
                   currentAngle = targetAngle;
                   targetHeight = targetCenter.y + height;
       
                   // Damp the height
                   float currentHeight = cameraTransform.position.y;
                   currentHeight = Mathf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, heightSmoothLag);
       
                   // Convert the angle into a rotation, by which we then reposition the camera
                   Quaternion currentRotation = Quaternion.Euler(0, currentAngle, 0);
       
                   // Set the position of the camera on the x-z plane to:
                   // distance meters behind the target
                   cameraTransform.position = targetCenter;
                   cameraTransform.position += currentRotation * Vector3.back * distance;
       
                   // Set the height of the camera
                   cameraTransform.position = new Vector3(cameraTransform.position.x, currentHeight, cameraTransform.position.z);
       
                   // Always look at the target    
                   SetUpRotation(targetCenter);
               }
       
               /// <summary>
               /// Directly position the camera to a the specified Target and center.
               /// </summary>
               void Cut()
               {
                   float oldHeightSmooth = heightSmoothLag;
                   heightSmoothLag = 0.001f;        
                   Apply();        
                   heightSmoothLag = oldHeightSmooth;
               }
       
               /// <summary>
               /// Sets up the rotation of the camera to always be behind the target
               /// </summary>
               /// <param name="centerPos">Center position.</param>
               void SetUpRotation(Vector3 centerPos)
               {
                   Vector3 cameraPos = cameraTransform.position;
                   Vector3 offsetToCenter = centerPos - cameraPos;        
       
                   // Generate base rotation only around y-axis
                   Quaternion yRotation = Quaternion.LookRotation(new Vector3(offsetToCenter.x, 0, offsetToCenter.z));        
       
                   Vector3 relativeOffset = Vector3.forward * distance + Vector3.down * height;
                   cameraTransform.rotation = yRotation * Quaternion.LookRotation(relativeOffset);        
       
               }
               #endregion
           }
       }
      
    3. 保存脚本

    如果你刚刚开始与实时3d,向量和四元数学,那么跟随玩家角色的数学是复杂的。所以我尝试在本教程中解释这一点。但是如果你好奇,想学习,不要犹豫与我们联系,我们会尽最大努力解释清楚。

    然而,这个脚本不仅仅是疯狂的数学,设置也是重要的,以及控制何时应该主动跟随玩家的能力。理解这点很重要:我们为什么要控制什么时候跟随玩家。

    通常,让我们想象如果总是跟随玩家会发生什么。当你连接到一个充满玩家的房间,其他玩家的每个CameraWork脚本为了一直看着自己的角色会相互打架...我们不想要这种,我们只想跟随本地玩家角色,它代表的是计算机后面的用户。

    一旦我们定义了这个问题,我们只有一个摄像机,但有多个Player实例,我们可以很容易找到几种方法。

    • 仅在本地玩家上附加CameraWork脚本。
    • 通过关闭和打开来控制CameraWork行为,具体取决于要关注的Player是否是本地玩家。
    • 让CameraWork连接到相机,并注意当场景中有一个本地玩家实例时只跟随那个。

    这3个选项并不详尽,可以找到更多的方法,但在这3个选项中,我们将任意选择第二个。上面的选项没有更好或更坏,但是这是一个可能需要最少的编码和最灵活的...“有趣...”我听到你说:)

    • 我们暴露了一个公共属性followOnStart,如果我们想在非联网环境中使用它,我们可以设置为true,例如在我们的测试场景中,或者在完全不同的场景

    • 当在我们的网络游戏中运行时,当我们检测到Player是本地玩家时,我们将调用公共方法OnStartFollowing()。这将在PlayerManager脚本中完成,该脚本PlayerManager在“Player Prefab Networking”一章中创建和解释。

    原文

    http://doc.photonengine.com/en-us/pun/current/tutorials/pun-basics-tutorial/player-camera-work

    相关文章

      网友评论

        本文标题:Photon Unity Networking基础教程 6 -

        本文链接:https://www.haomeiwen.com/subject/zrjwuttx.html