多人游戏的相机旋转

时间:2017-04-03 19:21:18

标签: c# unity3d camera unity5

我正在努力使用"鼠标外观/轨道"脚本,但我有问题。我的游戏有四个摄像头。它们以桌子为中心,代表一个玩家。这是一款多人纸牌游戏,所以你可以想象一下四面桌旁的玩家。每个相机都面向不同的方向。在我的检查器中,我有一个空对象,我将此脚本绑定到。在这个物体里面,是我的相机(所以,四个物体,每个物体都包含一个相机)。这是脚本:

public class MouseOrbitImproved : MonoBehaviour
{
    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 3F;
    public float sensitivityY = 3F;
    public float minimumX = -360F;
    public float maximumX = 360F;
    public float minimumY = -10.0F;
    public float maximumY = 60F;
    public string playerCamera;

    float rotationX = 0F;
    float rotationY = 0F;
    Quaternion originalRotation;
    private Rigidbody rigidBody;
    public float _distance = 20.0f;


    //Control the speed of zooming and dezooming.
    public float _zoomStep = 1.0f;

    void Update()
    {

        if (Input.GetMouseButton(2))
        {
            if (axes == RotationAxes.MouseXAndY)
            {
                // Read the mouse input axis
                rotationX += Input.GetAxis("Mouse X") * sensitivityX;
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationX = ClampAngle(rotationX, minimumX, maximumX);
                rotationY = ClampAngle(rotationY, minimumY, maximumY);
                Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
                Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, -Vector3.right);
                transform.localRotation = originalRotation * xQuaternion * yQuaternion;
            }
            else if (axes == RotationAxes.MouseX)
            {
                rotationX += Input.GetAxis("Mouse X") * sensitivityX;
                rotationX = ClampAngle(rotationX, minimumX, maximumX);
                Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
                transform.localRotation = originalRotation * xQuaternion;
            }
            else
            {
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY = ClampAngle(rotationY, minimumY, maximumY);
                Quaternion yQuaternion = Quaternion.AngleAxis(-rotationY, Vector3.right);
                transform.localRotation = originalRotation * yQuaternion;
            }
        }       

    }
    void Start()
    {
        // Make the rigid body not change rotation
        string camName = playerCamera + "CameraHolder";     
         GameObject eastCamera = GameObject.FindWithTag(camName);
        if (eastCamera)
        {
            originalRotation = eastCamera.transform.localRotation;

        }
    }
    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
             angle += 360F;
        if (angle > 360F)
             angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }
}

这个脚本适用于第一个播放器,但对于其他所有人来说,相机旋转的轴会混淆(我假设因为它像原来的#34;镜像&#34;)。那么,如何使用此脚本来说明相机指向的方向变化?我已尝试在“启动”功能中对此进行说明,但这似乎没有帮助。

感谢

修改

在与HRISTO合作之后,这就是我最终做的事情:

  1. 我停止使用四个摄像头,只使用了一个。然后我为四个&#34;玩家&#34;
  2. 中的每一个都有一个方向和旋转的矢量数组
  3. 当游戏加载时,我会根据加入游戏的玩家将相机设置为正确的旋转/方向阵列。
  4. 在我的相机脚本中,我添加了:

    private void LateUpdate()
    {
        GameObject controller = GameObject.FindWithTag("controller");
        GameController playerScript = controller.GetComponent<GameController>();
        int playerId = playerScript.getPlayerId();            
        string playerDirection = playerScript.getPlayerDirectionByID(playerId);
        DebugConsole.Log("Player Direction = " + playerDirection);
    
        if (playerDirection == "EAST")
        {
            originalRotation = playerScript.eastCameraRotaion;
            originalPosition = playerScript.eastCameraVector;
        }
        else if (playerDirection == "WEST")
        {
            originalRotation = playerScript.westCameraRotaion;
            originalPosition = playerScript.westCameraVector;
        }
        else if (playerDirection == "NORTH")
        {
            originalRotation = playerScript.northCameraRotaion;
            originalPosition = playerScript.northCameraVector;
        }
        else if (playerDirection == "SOUTH")
        {
            originalRotation = playerScript.southCameraRotaion;
            originalPosition = playerScript.southCameraVector;
        }    
    

    }

  5. 最初,我在Start方法中执行此操作,但在玩家加入游戏之前,已经意识到在加载场景时相机脚本已加载。因此,无法知道使用哪些向量。通过在Update方法中执行此操作,我可以检查要使用的方向/旋转。

0 个答案:

没有答案