| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using UnityEngine.UI;
- public class AimCrossHair : MonoBehaviour
- {
- public Camera mainCamera;
- [HideInInspector]
- public PlayerType playerType = PlayerType.FirstPlayer;
- [NonSerialized] public bool outboundLeft;
- [NonSerialized] public bool outboundRight;
- [NonSerialized] public bool outboundUp;
- [NonSerialized] public bool outboundDown;
- private bool visiable = false;
- void Start()
- {
- int currentShootLevel = UserSettings.ins.shootLevel;
- shootBackTime = new int[] { 0, 2, 5 }[currentShootLevel];
- shootOffsetAngleScale = new float[] { 0, 10f, 10f }[currentShootLevel];
- }
- void OnDestroy()
- {
- }
- void Update()
- {
- SetVisiable(onlyShow && GameController.ins.GetArmBowDoublePlayer(playerType).IsCanShoot());
- //if (GetPlayerIndex() == 0)
- //{
- // UpdatePositionByModuleRotation(mouseTest.Update(0),new Vector4(0,Screen.width*0.5f,Screen.height,0));
- //}
- //else if (GetPlayerIndex() == 1)
- //{
- // UpdatePositionByModuleRotation(mouseTest.Update(1), new Vector4(Screen.width*0.5f, Screen.width, Screen.height, 0));
- //}
- //if (GameController.ins.gameStart && !GameController.ins.gameOver)
- //{
- // transform.localScale = Vector3.one;
- //}
- //else transform.localScale = Vector3.zero;
- }
- private bool onlyShow = true;
- public void SetOnlyShow(bool onlyShow)
- {
- this.onlyShow = onlyShow;
- }
- public bool GetOnlyShow()
- {
- return this.onlyShow;
- }
- int GetPlayerIndex()
- {
- return playerType == PlayerType.FirstPlayer ? 0 : 1; //Array.IndexOf(GameController.ins.aimCrossHairs, this);
- }
- //MouseTest mouseTest = new MouseTest();
- class MouseTest
- {
- Quaternion quat = Quaternion.identity;
- float moveSensitivity = 30;
- public Quaternion Update(int type)
- {
- if (type == 0)
- {
- if (Input.GetKey(KeyCode.A))
- {
- quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.up) * quat;
- }
- else if (Input.GetKey(KeyCode.D))
- {
- quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.up) * quat;
- }
- else if (Input.GetKey(KeyCode.W))
- {
- quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.right) * quat;
- }
- else if (Input.GetKey(KeyCode.S))
- {
- quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.right) * quat;
- }
- }
- else if (type == 1)
- {
- if (Input.GetKey(KeyCode.LeftArrow))
- {
- quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.up) * quat;
- }
- else if (Input.GetKey(KeyCode.RightArrow))
- {
- quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.up) * quat;
- }
- else if (Input.GetKey(KeyCode.UpArrow))
- {
- quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.right) * quat;
- }
- else if (Input.GetKey(KeyCode.DownArrow))
- {
- quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.right) * quat;
- }
- }
- return quat;
- }
- }
- Quaternion currentRotation = Quaternion.identity;
- public void UpdatePositionByModuleRotation(Quaternion moduleRotation,Vector4 bound)
- {
- // 限制模块欧拉角范围
- Vector3 moduleAngles = moduleRotation.eulerAngles;
- moduleAngles.x = Mathf.Clamp(moduleAngles.x > 180 ? moduleAngles.x - 360 : moduleAngles.x, -80, 80);
- moduleAngles.y = Mathf.Clamp(moduleAngles.y > 180 ? moduleAngles.y - 360 : moduleAngles.y, -80, 80);
- moduleRotation = Quaternion.Euler(moduleAngles);
- currentRotation = moduleRotation;
- // 计算准心的世界旋转
- Quaternion rotation = mainCamera.transform.rotation * moduleRotation;
- // 计算准心的世界方向
- Vector3 direction = rotation * Vector3.forward;
- // 计算准心的世界坐标
- Vector3 position = mainCamera.transform.position + direction;
- // 转为屏幕坐标
- Vector3 screenPos = mainCamera.WorldToScreenPoint(position);
- // 标记是否超出屏幕边界
- outboundLeft = screenPos.x < bound.x;//0
- outboundRight = screenPos.x > bound.y;//Screen.width
- outboundUp = screenPos.y > bound.z;//Screen.height
- outboundDown = screenPos.y < bound.w; //0
- // 根据需要的范围进行限制
- screenPos.x = Mathf.Clamp(screenPos.x, bound.x, bound.y); //x->y
- screenPos.y = Mathf.Clamp(screenPos.y, bound.w, bound.z); //w->z
- // 设置准心的位置
- //transform.position = screenPos;
- }
- //射箭姿态记录索引倒退数,根据射击难度制造误差
- [NonSerialized] public int shootBackTime = 0;
- [NonSerialized] public float shootOffsetAngleScale = 0;
- //过去几帧的镜头值记录
- // Quaternion[] cameraRotations = new Quaternion[6];
- // int cameraRotationHasRecordCount = 0;
- public RaycastHit GetRaycastHit()
- {
- float maxDistance = 100;
- int layerMask = 1 << 8; //TargetLayerMask
- if (GetPlayerIndex() == 0)
- {
- layerMask = 1 << 6; // Target1
- }
- else if (GetPlayerIndex() == 1)
- {
- layerMask = 1 << 7; // Target2
- }
- RaycastHit raycastHit;
- Ray ray = mainCamera.ScreenPointToRay(crosshairTran.position, Camera.MonoOrStereoscopicEye.Mono);
- Debug.DrawRay(ray.origin, ray.direction * 1000, Color.yellow,5);
- Physics.Raycast(ray.origin, ray.direction, out raycastHit, maxDistance, layerMask);
- //UnityEditor.EditorApplication.isPaused = true;
- return raycastHit;
- }
- private void ChangeLayer(Transform transform, string layer)
- {
- if (transform.childCount > 0)//如果子物体存在
- {
- for (int i = 0; i < transform.childCount; i++)//遍历子物体是否还有子物体
- {
- ChangeLayer(transform.GetChild(i), layer);//这里是只将最后一个无子物体的对象设置层级
- }
- transform.gameObject.layer = LayerMask.NameToLayer(layer);//将存在的子物体遍历结束后需要把当前子物体节点进行层级设置
- }
- else //无子物体
- {
- transform.gameObject.layer = LayerMask.NameToLayer(layer);
- }
- if (transform.name == "_hunse_jian") {
- Debug.Log(transform.gameObject.layer);
- }
- }
- #region 更新中心准心
- RectTransform _parentRTF;
- RectTransform parentRTF
- {
- get
- {
- if (!_parentRTF)
- {
- _parentRTF = transform.parent.GetComponent<RectTransform>();
- }
- return _parentRTF;
- }
- }
- Vector3 centerPoint = new Vector3(0, 1, 0);
- Vector3 targetPosition;
- public Transform crosshairTran;
- public void UpdatePostionWhenFixedCamera(BowCameraDoublePlayer bowCameraDoublePlayer)
- {
- //if (!visiable) return;
- Transform transform = bowCameraDoublePlayer.transform;
- Ray ray = new Ray(transform.position, transform.forward);
- RaycastHit hitInfo;
- if (Physics.Raycast(ray, out hitInfo, 100))
- {
- targetPosition = hitInfo.point;
- }
- else
- {
- targetPosition = transform.position + transform.forward * 100f;
- }
- Vector3 v3 = bowCameraDoublePlayer.bowCameraFixed.camera.WorldToViewportPoint(targetPosition) - centerPoint;
- v3.x *= parentRTF.rect.width * 0.5f;
- v3.y *= parentRTF.rect.height;
- v3.z = 0;
- crosshairTran.localPosition = v3;
- action_UpdatePostionWhenFixedCamera?.Invoke();
- }
- public System.Action action_UpdatePostionWhenFixedCamera;
- //设置准心显示或者隐藏
- void SetVisiable(bool value)
- {
- if (this.visiable == value) return;
- this.visiable = value;
- crosshairTran.GetComponent<Image>().enabled = this.visiable;
- }
- /// <summary>
- /// 同步GetOnlyShow
- /// </summary>
- public void SyncVisiable()
- {
- this.visiable = GetOnlyShow();
- crosshairTran.GetComponent<Image>().enabled = this.visiable;
- }
- public void openDisplayToCenter()
- {
- if (GetPlayerIndex() == 0)
- {
- crosshairTran.transform.localPosition = new Vector3(0.25f * parentRTF.rect.width, -parentRTF.rect.height * 0.5f, 0);
- }
- else if (GetPlayerIndex() == 1)
- {
- crosshairTran.transform.localPosition = new Vector3(0.25f * parentRTF.rect.width, -parentRTF.rect.height * 0.5f, 0);
- }
- }
- #endregion
- }
|