| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [Serializable]
- //public struct CameraRect
- //{
- // public Rect Up;
- // public Rect Down;
- // public Rect Left;
- // public Rect Right;
- //}
- public class CameraController : MonoBehaviour
- {
- public float offsetX = 2, offsetY = 2, offsetZ = -17,totalOffsetYDown = -10;
- //public float splitOffsetZ = -20;
- public float lerpValue = 3.5f;
- //[HideInInspector]
- //public float offsetYDown;
- [HideInInspector]
- public Vector3 targetPos;
- //[HideInInspector]
- //public Vector3 player0TargetPos;
- //[HideInInspector]
- //public Vector3 player1TargetPos;
- [HideInInspector]
- public Vector3 cameraPos;
- public Camera mainCamera;
- //public Camera player0Camera;
- //public Camera player1Camera;
- //[HideInInspector]
- //public float maxView = 20;
- //[HideInInspector]
- //public float view;
- //public float splitDistance = 30;
- //public float mergeDistanceX = 16;
- //public float mergeDistanceY = 8;
- [HideInInspector]
- public float distanceX;
- [HideInInspector]
- public float distanceY;
- //[HideInInspector]
- //public float ratio;
- public float ratioY = 1;
- //public float lineMaxScale = 0.5f;
- //public GameObject lineInCamera;
- //public bool isSplit;
- //public bool isSplitY;
- //public float minDis;
- private int once;
- //[HideInInspector]
- //public Quaternion targetRotation;
- //[HideInInspector]
- //public Rect targetViewport0;
- //[HideInInspector]
- //public Rect targetViewport1;
- //public CameraRect cameraRect;
- private void Start()
- {
- //maxView = offsetZ - splitOffsetZ;
- //ratio = maxView / (splitDistance - mergeDistanceX);
- //offsetYDown = totalOffsetYDown;
- }
- private void Update()
- {
- //调参用
- //ratio = maxView / (splitDistance - minDistance);
- //
- PlayerController player0 = PlayersInput.instance[0];
- //PlayerController player1 = PlayersInput.instance[1];
- //if (player0!= null && player0!=player1)
- if (player0 != null)
- {
- Vector3 player0Pos = player0.transform.position;
- ////玩家距离接近且都不动时回蓝速度加快
- //if (once < 2 && distanceX <= minDis && distanceY <= minDis)
- //{
- // if (once == 0 && player0.state == CharacterState.Idle && player1.state == CharacterState.Idle)
- // {
- // once = 1;
- // player0.RapidReplyMp();
- // player1.RapidReplyMp();
- // }
- // else if (once == 1 && (player0.state != CharacterState.Idle || player1.state != CharacterState.Idle))
- // {
- // once = 2;
- // player0.NormalReplyMp();
- // player1.NormalReplyMp();
- // }
- //}
- //else if (once != 0 && distanceX <= minDis && distanceY <= minDis)
- //{
- // once = 0;
- // player0.NormalReplyMp();
- // player1.NormalReplyMp();
- //}
- ////根据玩家距离分屏/合屏
- //if (distanceX < mergeDistanceX)
- //{
- // if (isSplit)
- // {
- // view = 0;
- // CameraSplit(true);
- // }
- // else
- // {
- // if (distanceY > mergeDistanceY)
- // {
- // view = (distanceY - mergeDistanceY) * ratioY;
- // //isSplitY = true;
- // }
- // else
- // {
- // //if (isSplitY)
- // //{
- // // isSplitY = false;
- // //}
- // if(player0.isRevive || player1.isRevive)
- // {
- // offsetYDown = 0;
- // }
- // else
- // {
- // offsetYDown = Mathf.Clamp(totalOffsetYDown / (2 * mergeDistanceY) * distanceY,
- // totalOffsetYDown, 0);
- // }
-
- // }
-
- // }
- //}
- //else if (distanceX > splitDistance)
- //{
- // if (!isSplit)
- // {
- // if (player0Pos.x < player1Pos.x)
- // {
- // player0Camera.rect = new Rect(0, 0, 0.5f, 1);
- // player1Camera.rect = new Rect(0.5f, 0, 0.5f, 1);
- // }
- // else
- // {
- // player0Camera.rect = new Rect(0.5f, 0, 0.5f, 1);
- // player1Camera.rect = new Rect(0, 0, 0.5f, 1);
- // }
- // CameraSplit(false);
- // }
-
- //}
- //else
- //{
- // if (isSplit)
- // {
- // view = 0;
- // }
- // else
- // {
- // float viewX = Mathf.Clamp((distanceX - mergeDistanceX) * ratio, 0, maxView);
- // float viewY = (distanceY - mergeDistanceY) * ratioY;
- // if(viewX > viewY)
- // {
- // view = viewX;
- // //if (isSplitY)
- // //{
- // // isSplitY = false;
- // //}
- // }
- // else
- // {
- // view = viewY;
- // //isSplitY = true;
-
- // }
- // if (player0.isRevive || player1.isRevive)
- // {
- // offsetYDown = 0;
- // }
- // else
- // {
- // offsetYDown = Mathf.Clamp(totalOffsetYDown / (2 * mergeDistanceY) * distanceY,
- // totalOffsetYDown, 0);
- // }
-
- // }
-
- //}
- //if (isSplit)
- //{
- // lineInCamera.transform.localScale = new Vector3(
- // Mathf.Clamp((distanceX - mergeDistanceX) * 0.01f, 0,
- // lineMaxScale), 20, 1);
- //}
- }
- }
- private void FixedUpdate()
- {
- PlayerController player0 = PlayersInput.instance[0];
- //PlayerController player1 = PlayersInput.instance[1];
- if (player0 == null)
- {
- return;
- }
- else
- {
- if (player0.isRevive)
- {
- targetPos = new Vector3(140, offsetY, offsetZ);
- }
- else
- {
- targetPos = CameraTargetMove(player0);
- }
- if(Time.timeScale == 1)
- {
- mainCamera.transform.position =
- Vector3.Lerp(mainCamera.transform.position, targetPos, lerpValue * Time.deltaTime);
- }
-
- }
- //else if (player1 == player0)
- //{
- // if (player0.isRevive)
- // {
- // targetPos = new Vector3(140, offsetY, offsetZ);
- // }
- // else
- // {
- // targetPos = CameraTargetMove(player0);
- // }
- // mainCamera.transform.position =
- // Vector3.Lerp(mainCamera.transform.position, targetPos, lerpValue * Time.deltaTime);
- //}
- //else
- //{
- // player0TargetPos = CameraTargetMove(player0);
- // player1TargetPos = CameraTargetMove(player1);
- // if (player0.isRevive && player1.isRevive)
- // {
- // player0TargetPos = new Vector3(140, offsetY, offsetZ);
- // player1TargetPos = new Vector3(140, offsetY, offsetZ);
- // view = 0;
- // if (isSplit)
- // {
- // CameraSplit(true);
- // }
- // }
- // else if (player0.isRevive)
- // {
- // player0TargetPos = CameraTargetMove(player1);
- // view = 0;
- // if (isSplit)
- // {
- // CameraSplit(true);
- // }
- // }
- // else if (player1.isRevive)
- // {
- // player1TargetPos = CameraTargetMove(player0);
- // view = 0;
- // if (isSplit)
- // {
- // CameraSplit(true);
- // }
- // }
- // if (player0TargetPos.y > player1TargetPos.y)
- // {
- // player0Camera.transform.position = Vector3.Lerp(player0Camera.transform.position,
- // player0TargetPos + Vector3.up * offsetYDown, lerpValue * 2 * Time.deltaTime);
- // player1Camera.transform.position = Vector3.Lerp(player1Camera.transform.position,
- // player1TargetPos, lerpValue * 2 * Time.deltaTime);
- // }
- // else
- // {
- // player0Camera.transform.position = Vector3.Lerp(player0Camera.transform.position,
- // player0TargetPos, lerpValue * 2 * Time.deltaTime);
- // player1Camera.transform.position = Vector3.Lerp(player1Camera.transform.position,
- // player1TargetPos + Vector3.up * offsetYDown, lerpValue * 2 * Time.deltaTime);
- // }
- // mainCamera.transform.position = Vector3.Lerp(player0Camera.transform.position,
- // player1Camera.transform.position, 0.5f);
- //}
- }
- //public void CameraSplit(bool flag)
- //{
- // if (flag)
- // {
-
- // mainCamera.enabled = true;
- // player0Camera.enabled = false;
- // player1Camera.enabled = false;
- // lineInCamera.SetActive(false);
- // isSplit = false;
- // }
- // else
- // {
- // if(PlayersInput.instance[0].isRevive || PlayersInput.instance[1].isRevive)
- // {
- // return;
- // }
- // offsetYDown = 0;
- // mainCamera.enabled = false;
- // player0Camera.enabled = true;
- // player1Camera.enabled = true;
- // lineInCamera.SetActive(true);
- // isSplit = true;
- // }
- //}
- Vector3 CameraTargetMove(PlayerController player)
- {
- Vector3 skewPos;
- if (player.bodyTrans.localScale.x > 0)
- {
- skewPos = player.transform.position + new Vector3(-offsetX, offsetY, offsetZ);
- }
- else
- {
- skewPos = player.transform.position + new Vector3(offsetX, offsetY, offsetZ);
- }
-
- //if (isSplit)
- //{
- // if (player.bodyTrans.localScale.x > 0)
- // {
- // skewPos = player.transform.position + new Vector3(-offsetX / 2, offsetY, splitOffsetZ);
- // }
- // else
- // {
- // skewPos = player.transform.position + new Vector3(offsetX / 2, offsetY, splitOffsetZ);
- // }
- //}
- //else
- //{
- // skewPos = player.transform.position + new Vector3(0, offsetY, offsetZ - view);
- //}
- //skewPos = player.transform.position + new Vector3(0, offsetY, offsetZ);
- return skewPos;
- }
- }
|