【Unity】 相机移动脚本
发表于2017-08-28
下面给大家介绍Unity 中的相机移动脚本,由于在项目制作中经常要对相机进行移动,所以采用了公司的工具类CameraOrbit 和 CameraControl,
在项目中需要添加 FingerGestures 插件, 相机脚本是基于FingerGestures 上改写 并搭配,
在相机物体中添加
Root 和Orgin 是和相机相同位置的 相机的移动也是基于 root 和origin的
以下是CameraOrbit代码
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- ///
- /// Adaptation of the standard MouseOrbit script to use the finger drag gesture to rotate the current object using
- /// the fingers/mouse around a target object
- ///
- public class XgCameraOrbit : MonoBehaviour
- {
- public enum PanMode
- {
- Disabled,
- OneFinger,
- TwoFingers
- }
- ///
- /// The object to orbit around
- ///
- public Transform target;
- public Transform camRoot;
- public Transform camOrigin;
- private XgCameraManager xg_Cm;
- //private XgIntellControl xg_IC;
- //private XgPictrueControl xg_Pc;
- ///
- /// Initial camera distance to target
- ///
- public float initialDistance = 10.0f;
- ///
- /// Minimum distance between camera and target
- ///
- public float minDistance = 1.0f;
- ///
- /// Maximum distance between camera and target
- ///
- public float maxDistance = 30.0f;
- ///
- /// Affects horizontal rotation speed (in degrees per cm)
- ///
- public float yawSensitivity = 45.0f;
- ///
- /// Affects vertical rotation speed (in degrees per cm)
- ///
- public float pitchSensitivity = 45.0f;
- ///
- /// Keep yaw angle value between minYaw and maxYaw?
- ///
- public bool clampYawAngle = false;
- public float minYaw = -75;
- public float maxYaw = 75;
- ///
- /// Keep pitch angle value between minPitch and maxPitch?
- ///
- public bool clampPitchAngle = true;
- public float minPitch = -20;
- public float maxPitch = 80;
- ///
- /// Allow the user to affect the orbit distance using the pinch zoom gesture
- ///
- public bool allowPinchZoom = true;
- ///
- /// Affects pinch zoom speed
- ///
- public float pinchZoomSensitivity = 5.0f;
- private float pinchZoomSensMax;
- private float pinchZoomSens;
- ///
- /// Use smooth camera motions?
- ///
- public bool smoothMotion = true;
- public float smoothZoomSpeed = 5.0f;
- public float smoothOrbitSpeed = 10.0f;
- ///
- /// Two-Finger camera panning.
- /// Panning will apply an offset to the pivot/camera target point
- ///
- public bool allowPanning = false;
- public bool invertPanningDirections = false;
- public float panningSensitivity = 1.0f;
- private float panningSensMax;
- private float panningSens;
- public Transform panningPlane;
- // reference transform used to apply the panning translation (using panningPlane.right and panningPlane.up vectors)
- public bool smoothPanning = true;
- public float smoothPanningSpeed = 12.0f;
- // collision test
- public LayerMask collisionLayerMask;
- private Transform myTrans;
- private float distance = 10.0f;
- private float yaw = 0;
- private float pitch = 0;
- private float idealDistance = 0;
- private float idealYaw = 0;
- private float idealPitch = 0;
- private Vector3 idealPanOffset = Vector3.zero;
- private Vector3 panOffset = Vector3.zero;
- private Vector3 rPanOffset;
- private bool slideCentered = false;
- private Vector3 slideOffset;
- private PinchRecognizer pinchRecognizer;
- private bool UIdrag = false;
- private bool canControl = true;
- private bool canInput = true;
- private float fl_MovePos = 0f;
- private float fl_IdeaMovePos;
- bool canPan;
- bool canPanY;
- bool canPanX;
- //messager
- //SLCameraMessager sCm;
- //调用
- [HideInInspector]
- //public HRMainControl main_control;
- void Start()
- {
- //main_control = GameObject.Find("Control").GetComponent
(); - }
- public bool UIDrag
- {
- get { return UIdrag; }
- set { UIdrag = value; }
- }
- public float Distance
- {
- get { return distance; }
- }
- public float IdealDistance
- {
- get { return idealDistance; }
- set { idealDistance = Mathf.Clamp(value, minDistance, maxDistance); }
- }
- public float Yaw
- {
- get { return yaw; }
- }
- public float IdealYaw
- {
- get { return idealYaw; }
- set { idealYaw = clampYawAngle ? ClampAngle(value, minYaw, maxYaw) : value; }
- }
- public float Pitch
- {
- get { return pitch; }
- }
- public float IdealPitch
- {
- get { return idealPitch; }
- set { idealPitch = clampPitchAngle ? ClampAngle(value, minPitch, maxPitch) : value; }
- }
- public Vector3 IdealPanOffset
- {
- get { return idealPanOffset; }
- set { idealPanOffset = value; }
- }
- public Vector3 PanOffset
- {
- get { return panOffset; }
- }
- public Vector3 ROffset
- {
- get { return rPanOffset; }
- }
- private void InstallGestureRecognizers()
- {
- //xg_IC = GameObject.Find(str_Intell_Control).GetComponent
(); - //xg_Pc = GameObject.Find(str_Pic_Control).GetComponent
(); - List
recogniers = new List (GetComponents ()); - DragRecognizer drag = recogniers.Find(r => r.EventMessageName == "OnDrag") as DragRecognizer;
- DragRecognizer twoFingerDrag =
- recogniers.Find(r => r.EventMessageName == "OnTwoFingerDrag") as DragRecognizer;
- PinchRecognizer pinch = recogniers.Find(r => r.EventMessageName == "OnPinch") as PinchRecognizer;
- // check if we need to automatically add a screenraycaster
- if (OnlyRotateWhenDragStartsOnObject)
- {
- ScreenRaycaster raycaster = gameObject.GetComponent
(); - if (!raycaster)
- raycaster = gameObject.AddComponent
(); - }
- if (!drag)
- {
- drag = gameObject.AddComponent
(); - drag.RequiredFingerCount = 1;
- drag.IsExclusive = true;
- drag.MaxSimultaneousGestures = 1;
- drag.SendMessageToSelection = GestureRecognizer.SelectionType.None;
- }
- if (!pinch)
- pinch = gameObject.AddComponent
(); - if (!twoFingerDrag)
- {
- twoFingerDrag = gameObject.AddComponent
(); - twoFingerDrag.RequiredFingerCount = 2;
- twoFingerDrag.IsExclusive = true;
- twoFingerDrag.MaxSimultaneousGestures = 1;
- twoFingerDrag.ApplySameDirectionConstraint = true;
- twoFingerDrag.EventMessageName = "OnTwoFingerDrag";
- }
- }
- public void Init_Camera()
- {
- xg_Cm = gameObject.GetComponent
(); - if (xg_Cm != null)
- xg_Cm.Init_CameraManager();
- InstallGestureRecognizers();
- myTrans = transform;
- if (!panningPlane)
- panningPlane = myTrans;
- Vector3 angles = myTrans.eulerAngles;
- distance = IdealDistance = initialDistance;
- yaw = IdealYaw = angles.y;
- pitch = IdealPitch = angles.x;
- pinchZoomSensMax = pinchZoomSensitivity;
- panningSensMax = panningSensitivity;
- // Make the rigid body not change rotation
- if (GetComponent
()) - GetComponent
().freezeRotation = true; - Apply();
- }
- #region Gesture Event Messages
- private float nextDragTime = 0.8f;
- public bool OnlyRotateWhenDragStartsOnObject = false;
- private void OnDrag(DragGesture gesture)
- {
- if (canControl)
- {
- //Debug.Log("22222");
- if (!canInput) return;
- if (!UIdrag)
- {
- // don't rotate unless the drag started on our target object
- if (OnlyRotateWhenDragStartsOnObject)
- {
- if (gesture.Phase == ContinuousGesturePhase.Started)
- {
- if (!gesture.Recognizer.Raycaster)
- {
- Debug.LogWarning("The drag recognizer on " gesture.Recognizer.name
- " has no ScreenRaycaster component set. This will prevent OnlyRotateWhenDragStartsOnObject flag from working.");
- OnlyRotateWhenDragStartsOnObject = false;
- return;
- }
- if (target && !target.GetComponent
()) - {
- Debug.LogWarning(
- "The target object has no collider set. OnlyRotateWhenDragStartsOnObject won't work.");
- OnlyRotateWhenDragStartsOnObject = false;
- return;
- }
- }
- if (!target || gesture.StartSelection != target.gameObject)
- return;
- }
- // wait for drag cooldown timer to wear off
- // used to avoid dragging right after a pinch or pan, when lifting off one finger but the other one is still on screen
- if (Time.time < nextDragTime)
- return;
- if (target)
- {
- IdealYaw = gesture.DeltaMove.x.Centimeters() * yawSensitivity;
- IdealPitch -= gesture.DeltaMove.y.Centimeters() * pitchSensitivity;
- //if (main_control.uiControl.pinghenghuan_Index == 1)
- //{
- // main_control.uiControl.bl_pinghenghuan = true;
- // main_control.uiControl.OpenPinghenghuan();
- //}
- //if (main_control.uiControl.dianji_Index == 1)
- //{
- // main_control.uiControl.bl_dianji = true;
- // main_control.uiControl.OpenDianJi();
- //}
- //--------------关闭技能计数-----------------
- //main_control.uiControl.isOpenCD = false;
- //StopAllCoroutines();
- ////如果有滑动的话 也会让 UI计数为false
- //main_control.uiControl.uiClick = false;
- //if (Input.GetMouseButtonUp(0))
- //{
- // if (main_control.uiControl.pinghenghuan_Index == 1)
- // {
- // main_control.uiControl.bl_pinghenghuan = false;
- // main_control.uiControl.OpenPinghenghuan();
- // }
- // if (main_control.uiControl.dianji_Index == 1)
- // {
- // main_control.uiControl.bl_dianji = false;
- // main_control.uiControl.OpenDianJi();
- // }
- // if (!main_control.uiControl.isMainUI)
- // {
- // main_control.uiControl.isOpenCD = true;
- // }
- //}
- //if (Input.GetMouseButtonUp(0) || UICamera)
- //{
- // main_control.uiControl.isOpenCD = true;
- //}
- }
- }
- }
- }
- private void OnPinch(PinchGesture gesture)
- {
- if (canControl)
- {
- if (!canInput) return;
- if (allowPinchZoom)
- {
- pinchZoomSens = Distance / 5.0f * pinchZoomSensMax;
- IdealDistance -= gesture.Delta.Centimeters() * pinchZoomSens;
- nextDragTime = Time.time 0.25f;
- }
- }
- }
- private void OnTwoFingerDrag(DragGesture gesture)
- {
- if (canControl)
- {
- if (!canInput) return;
- if (allowPanning)
- {
- panningSens = Distance / 5.0f * panningSensMax;
- Vector3 move = -panningSens *
- (panningPlane.right * gesture.DeltaMove.x.Centimeters()
- panningPlane.up * gesture.DeltaMove.y.Centimeters());
- if (invertPanningDirections)
- IdealPanOffset = -move;
- else
- IdealPanOffset = move;
- //reset idealPanPositon
- if (gesture.Phase == ContinuousGesturePhase.Ended)
- IdealPanOffset = Vector3.zero;
- nextDragTime = Time.time 0.25f;
- }
- }
- }
- private void OnTap(TapGesture gesture)
- {
- if (canControl && gesture.Selection)
- {
- xg_Cm.OnManagerTap(gesture);
- }
- //if (canControl && gesture.Selection)
- //{
- // if (gesture.Selection.CompareTag("MD_Main"))
- // {
- // main_control.
- // }
- //}
- }
- //IN assemble mode, double tap to auto disassemble
- private void OnDoubleTap(TapGesture gesture)
- {
- if (canControl)
- if (gesture.Selection)
- xg_Cm.OnManagerTap(gesture);
- }
- #endregion
- private void Apply()
- {
- if (smoothMotion)
- {
- distance = Mathf.Lerp(distance, IdealDistance, Time.deltaTime * smoothZoomSpeed);
- yaw = Mathf.LerpAngle(yaw, IdealYaw, Time.deltaTime * smoothOrbitSpeed);
- pitch = Mathf.LerpAngle(pitch, IdealPitch, Time.deltaTime * smoothOrbitSpeed);
- }
- else
- {
- distance = IdealDistance;
- yaw = IdealYaw;
- pitch = IdealPitch;
- }
- //print(IdealYaw);
- if (smoothPanning)
- panOffset = Vector3.Lerp(panOffset, idealPanOffset, Time.deltaTime * smoothPanningSpeed);
- else
- panOffset = idealPanOffset;
- if (canInput)
- rPanOffset = camOrigin.InverseTransformPoint(panOffset myTrans.position);
- //rotate the orignal camera
- camOrigin.rotation = Quaternion.Euler(pitch, yaw, 0);
- //change rotation to the orginal camera
- myTrans.rotation = camOrigin.rotation;
- camOrigin.position = target.position - camOrigin.forward * distance;
- //set position for camera
- myTrans.position = camOrigin.TransformPoint(rPanOffset);
- //Debug.Log(rPanOffset);
- }
- private void LateUpdate()
- {
- if (canControl)
- {
- Apply();
- }
- }
- public void CameraProperty(bool pan, bool zoom)
- {
- allowPanning = pan;
- allowPinchZoom = zoom;
- }
- private static float ClampAngle(float angle, float min, float max)
- {
- if (angle < -360)
- angle = 360;
- if (angle > 360)
- angle -= 360;
- return Mathf.Clamp(angle, min, max);
- }
- public void SlideCenter(bool enable, Vector3 offset)
- {
- slideCentered = enable;
- if (enable)
- {
- slideOffset = offset;
- }
- else
- {
- slideOffset = Vector3.zero;
- }
- }
- public void CanControl(bool enable)
- {
- canControl = enable;
- }
- // recenter the camera
- public void ResetPanning()
- {
- IdealPanOffset = Vector3.zero;
- }
- //获取角度
- public float GetCamYawAngle()
- {
- return yaw % 360 < 0 ? 360 yaw % 360 : Yaw % 360;
- }
- public float GetCamPitchAngle()
- {
- return pitch;
- }
- public IEnumerator PauseControl(float time)
- {
- canControl = false;
- yield return new WaitForSeconds(time 0.1f);
- canControl = true;
- }
- public void PauseInput(float time)
- {
- StopCoroutine("PauseCamInput");
- StartCoroutine("PauseCamInput", time);
- }
- private IEnumerator PauseCamInput(float time)
- {
- canInput = false;
- yield return new WaitForSeconds(time);
- canInput = true;
- }
- public void TeleportCam(Transform pivot, Vector3 originPos, Vector3 rot, float dis, Vector3 pan)
- {
- target = pivot;
- distance = idealDistance = dis;
- yaw = idealYaw = rot.y >= 180 ? rot.y - 360 : rot.y;
- pitch = idealPitch = rot.x >= 180 ? rot.x - 360 : rot.x;
- panOffset = idealPanOffset = Vector3.zero;
- rPanOffset = pan;
- camOrigin.position = originPos;
- camOrigin.eulerAngles = rot;
- }
- public void Cam_CoreComValue()
- {
- maxDistance = 20f;
- minDistance = 15f;
- maxPitch = 40;
- allowPanning = false;
- }
- public void Cam_DefaultValue()
- {
- maxDistance = 20f;
- minDistance = 12f;
- maxPitch = 40;
- minPitch = 0;
- clampYawAngle = false;
- allowPanning = false;
- }
- public void Cam_SellingPoint_BaoXianDeng()
- {
- minYaw = 10;
- maxYaw = 170;
- maxPitch = 60;
- minPitch = 1;
- minDistance = 1f;
- maxDistance = 10f;
- clampYawAngle = true;
- allowPanning = false;
- }
- public void Cam_SellingPoint_TiaoShiMoKuai()
- {
- maxDistance = 5f;
- minDistance = 1f;
- maxPitch = 80;
- minPitch = 0;
- clampYawAngle = false;
- allowPanning = false;
- }
- public void OnDistance(float dis)
- {
- //Debug.Log("distance: " dis);
- distance = dis;
- idealDistance = dis;
- }
- public void OnViewAngle(Vector3 angle)
- {
- //Debug.Log("angle: " angle);
- camOrigin.eulerAngles = angle;
- yaw = idealYaw = angle.y;
- pitch = idealPitch = angle.x;
- }
- public void OnComplete()
- {
- print("sdf");
- }
- public void PanAxis(bool canX, bool canY)
- {
- canPanX = canX;
- canPanY = canY;
- }
- public void OnPanOffset(Vector3 offset)
- {
- panOffset = idealPanOffset = Vector3.zero;
- rPanOffset = offset;
- }
- }
- using UnityEngine;
- using System.Collections;
- public class XgCameraControl : MonoBehaviour
- {
- public delegate void TravelCallback();
- public delegate void CACProcess();
- public float fl_Time;
- private float time_Shift = 0.8f;
- private float spd_Shift = 0.5f;
- private static iTween.EaseType posEaseType = iTween.EaseType.easeOutQuad;
- private static iTween.EaseType rotEaseType = iTween.EaseType.easeOutQuad;
- private XgCameraOrbit xGo;
- private void Awake()
- {
- xGo = GameObject.Find("Main Camera").GetComponent
(); - }
- ///
- /// 设置相机的Yaw
- ///
- ///
- public void CamAutoCorrection(float yaw)
- {
- xGo.IdealYaw = yaw;
- }
- ///
- /// 获取相机角度
- ///
- ///
- public float GetCamYawAngle()
- {
- return xGo.GetCamYawAngle();
- }
- public float GetCamPitchAngle()
- {
- return xGo.GetCamPitchAngle();
- }
- public void TravelToMenu(Transform cam, Transform center, TravelCallback callback = null, float time = -1f)
- {
- float hybirdPitch = 2f;
- float distance = 9;
- Vector3 rot = new Vector3(hybirdPitch, cam.eulerAngles.y, cam.eulerAngles.z);
- Transform temp = GameObject.FindGameObjectWithTag("camRoot").transform;
- Vector3 offset = Vector3.zero;
- temp.eulerAngles = rot;
- Vector3 pos = center.position - temp.forward * distance offset;
- StopAllCoroutines();
- time = time == -1f ? fl_Time : time;
- StartCoroutine(xGo.PauseControl(fl_Time));
- xGo.TeleportCam(center, pos, rot, distance, offset);
- Travel(cam, pos, rot, time);
- if (callback != null)
- StartCoroutine(Callback(time, callback));
- }
- private IEnumerator Callback(float time, TravelCallback callback)
- {
- yield return new WaitForSeconds(time);
- callback();
- }
- //public void TravelToPoint(Transform cam, Transform viewPoint, Transform center = null)
- //{
- // if (!center)
- // center = xGo.target.transform;
- // Vector3 pos = viewPoint.position;
- // viewPoint.LookAt(center);
- // Vector3 rot = viewPoint.eulerAngles;
- // Vector3 desiredPos = center.position - Vector3.Project(center.position - pos, viewPoint.forward);
- // float distance = Vector3.Distance(desiredPos, center.position);
- // //offset between new pos and the current position
- // Vector3 offset = pos - desiredPos;
- // StopAllCoroutines();
- // StartCoroutine(xGo.PauseControl(fl_Time));
- // xGo.TeleportCam(center, desiredPos, rot, distance, offset);
- // Travel(cam, pos, rot, fl_Time);
- //}
- public void TravelToPointNotDistance(Transform cam, Transform viewPoint, Transform tran_lookPos, Transform center = null, float time = -1f)
- {
- if (!center)
- center = xGo.target.transform;
- if (time == -1f)
- time = fl_Time;
- Vector3 pos = viewPoint.position;
- viewPoint.LookAt(tran_lookPos);
- Vector3 rot = viewPoint.eulerAngles;
- Vector3 desiredPos = center.position - Vector3.Project(center.position - pos, viewPoint.forward);
- float distance = Vector3.Distance(desiredPos, center.position);
- //offset between new pos and the current position
- Vector3 offset = pos - desiredPos;
- StopAllCoroutines();
- StartCoroutine(xGo.PauseControl(time));
- xGo.TeleportCam(center, desiredPos, rot, distance, offset);
- Travel(cam, pos, rot, time);
- }
- public void TravelToDistance(Transform cam, float distance, Transform center = null)
- {
- if (!center)
- center = xGo.target.transform;
- Vector3 pos = center.position - (center.position - cam.position).normalized * distance;
- Vector3 rot = CamEulerAngles(center.position - cam.position);
- StopAllCoroutines();
- StartCoroutine(xGo.PauseControl(fl_Time));
- //Transform.ReferenceEquals
- xGo.TeleportCam(center, pos, rot, distance, Vector3.zero);
- Travel(cam, pos, rot, fl_Time);
- }
- public void TravelWithDistanceOffset(Transform cam, float distance, Vector3 offset, Transform center = null)
- {
- if (!center)
- center = xGo.target.transform;
- Vector3 pos = center.position - (center.position - cam.position).normalized * distance;
- Vector3 rot = CamEulerAngles(center.position - cam.position);
- StopAllCoroutines();
- StartCoroutine(xGo.PauseControl(fl_Time));
- Transform temp = GameObject.FindGameObjectWithTag("camRoot").transform;
- temp.position = pos;
- temp.eulerAngles = rot;
- Vector3 v = temp.TransformPoint(offset);
- xGo.TeleportCam(center, pos, rot, distance, offset);
- Travel(cam, v, rot, fl_Time);
- }
- public void TravelWithPointOffset(Transform cam, Transform viewPoint, Vector3 offset, Transform center = null)
- {
- if (!center)
- center = xGo.target.transform;
- Vector3 pos = viewPoint.position;
- viewPoint.LookAt(center.position offset);
- Vector3 rot = viewPoint.eulerAngles;
- Vector3 desiredPos = center.position - Vector3.Project(center.position - pos, viewPoint.forward);
- float distance = Vector3.Distance(desiredPos, center.position);
- StopAllCoroutines();
- StartCoroutine(xGo.PauseControl(fl_Time));
- xGo.TeleportCam(center, desiredPos, rot, distance, offset);
- Travel(cam, pos, rot, fl_Time);
- }
- //public void TravelToPoint(Transform cam, Transform pos,Transform center,Vector3 v3_Offset=default(Vector3))
- //{
- // Transform camTrans = cam.transform;
- // Vector3 desiredPos = position - Vector3.Project(center.position - position, forward);
- // float newDistance = Vector3.Distance(desiredPos, center.position);
- // Vector3 newOffset = v3_Offset == Vector3.zero ? position - desiredPos : v3_Offset;
- // Vector3 newViewAngle = eulerAngles;
- // Transform oldPivot = xGo.target;
- // Vector3 oldViewAngle = camTrans.eulerAngles;
- // //print(newViewAngle - oldViewAngle);
- // float oldDistance = xGo.Distance;
- // Vector3 oldOffset = xGo.ROffset;
- // float time = time_Shift;
- // StopAllCoroutines();
- // xGo.PauseInput(time 0.1f);
- // //shifting
- // iTween.MoveTo(xGo.target.gameObject, iTween.Hash("position", center.position, "time", time, "easeType", posEaseType));
- // //shift view angles
- // float anplyYDiff = oldViewAngle.y - newViewAngle.y;
- // float anglyXDiff = oldViewAngle.x - newViewAngle.x;
- // if (Mathf.Abs(anplyYDiff) > 180)
- // newViewAngle = new Vector3(newViewAngle.x, oldViewAngle.y (anplyYDiff > 0 ? 360 - anplyYDiff : -(360 anplyYDiff)), newViewAngle.z);
- // if (Mathf.Abs(anglyXDiff) > 180)
- // newViewAngle = new Vector3(oldViewAngle.x (anglyXDiff > 0 ? 360 - anglyXDiff : -(360 anglyXDiff)), newViewAngle.y, newViewAngle.z);
- // iTween.ValueTo(cam.gameObject, iTween.Hash(
- // "from", oldViewAngle,
- // "to", newViewAngle,
- // "time", time,
- // "onupdatetarget", cam.gameObject,
- // "onupdate", "OnViewAngle",
- // "easetype", rotEaseType));
- // //shift view distance
- // iTween.ValueTo(cam.gameObject, iTween.Hash(
- // "from", oldDistance,
- // "to", newDistance,
- // "time", time,
- // "onupdatetarget", cam.gameObject,
- // "onupdate", "OnDistance",
- // "easetype", rotEaseType));
- // //shift view offset
- // iTween.ValueTo(cam.gameObject, iTween.Hash(
- // "from", oldOffset,
- // "to", newOffset,
- // "time", time,
- // "onupdatetarget", cam.gameObject,
- // "onupdate", "OnPanOffset",
- // "easetype", rotEaseType));
- //}
- public void TravelToPoint(Transform cam, Transform tran_Pos, Transform tran_Center, Vector3 v3_Offset = default(Vector3))
- {
- Transform camTrans = cam.transform;
- Vector3 desiredPos = tran_Center.position - Vector3.Project(tran_Center.position - tran_Pos.position, tran_Pos.forward);
- float newDistance = Vector3.Distance(desiredPos, tran_Center.position);
- Vector3 newOffset = v3_Offset == Vector3.zero ? tran_Pos.position - desiredPos : v3_Offset;
- Vector3 newViewAngle = tran_Pos.eulerAngles;
- Transform oldPivot = xGo.target;
- Vector3 oldViewAngle = camTrans.eulerAngles;
- //print(newViewAngle - oldViewAngle);
- float oldDistance = xGo.Distance;
- Vector3 oldOffset = xGo.ROffset;
- float time = fl_Time;
- StopAllCoroutines();
- xGo.PauseInput(time 0.1f);
- //shifting
- iTween.MoveTo(xGo.target.gameObject, iTween.Hash("position", tran_Center.position, "time", time, "easeType", posEaseType));
- //shift view angles
- float anplyYDiff = oldViewAngle.y - newViewAngle.y;
- float anglyXDiff = oldViewAngle.x - newViewAngle.x;
- if (Mathf.Abs(anplyYDiff) > 180)
- newViewAngle = new Vector3(newViewAngle.x, oldViewAngle.y (anplyYDiff > 0 ? 360 - anplyYDiff : -(360 anplyYDiff)), newViewAngle.z);
- if (Mathf.Abs(anglyXDiff) > 180)
- newViewAngle = new Vector3(oldViewAngle.x (anglyXDiff > 0 ? 360 - anglyXDiff : -(360 anglyXDiff)), newViewAngle.y, newViewAngle.z);
- iTween.ValueTo(cam.gameObject, iTween.Hash(
- "from", oldViewAngle,
- "to", newViewAngle,
- "time", time,
- "onupdatetarget", cam.gameObject,
- "onupdate", "OnViewAngle",
- "easetype", rotEaseType));
- //shift view distance
- iTween.ValueTo(cam.gameObject, iTween.Hash(
- "from", oldDistance,
- "to", newDistance,
- "time", time,
- "onupdatetarget", cam.gameObject,
- "onupdate", "OnDistance",
- "easetype", rotEaseType));
- //shift view offset
- iTween.ValueTo(cam.gameObject, iTween.Hash(
- "from", oldOffset,
- "to", newOffset,
- "time", time,
- "onupdatetarget", cam.gameObject,
- "onupdate", "OnPanOffset",
- "easetype", rotEaseType));
- }
- private Vector3 CamEulerAngles(Vector3 forward)
- {
- Quaternion q = Quaternion.LookRotation(forward);
- //Vector3 v = (fromPos - toPos).normalized;
- //Quaternion q = camTrams.rotation;
- //q.SetLookRotation(camTrams.forward, toPos);
- //Quaternion.FromToRotation(Vector3.up, transform.forward);
- return q.eulerAngles;
- }
- //public void TravelToPoint(Transform tran_Point, Transform center = null, Transform tran_Cam = null)
- //{
- // if (!center)
- // center = xGo.target.transform;
- // if (!tran_Cam)
- // tran_Cam = transform;
- // Vector3 v3_Pos = tran_Point.position;
- // Vector3 v3_Rot = tran_Point.eulerAngles;
- // Vector3 v3_DesiredPos = center.position -
- // Vector3.Project(center.position - v3_Pos, tran_Point.forward);
- // float fl_Distance = Vector3.Distance(v3_Pos, center.position);
- // Vector3 v3_Offset = v3_Pos - v3_DesiredPos;
- // StopAllCoroutines();
- // StartCoroutine(PauseMove(fl_Time));
- // xGo.TeleportCam(center, v3_DesiredPos, v3_Rot, fl_Distance, v3_Offset);
- // Travel(tran_Cam, v3_Pos, v3_Rot);
- //}
- private void Travel(Transform tran_Cam, Vector3 v3_Pos, Vector3 v3_Rot, float time)
- {
- XgControlTools.Move(tran_Cam, v3_Pos, time);
- XgControlTools.Rotate(tran_Cam, v3_Rot, time);
- }
- }
- [System.Serializable]
- public class TravelPosition
- {
- public Transform tran_Center;
- public Transform tran_Pos;
- public Vector3 v3_Offset;
- }
Manger控制
- <pre name=< span="">"code" class="csharp">using UnityEngine; </pre name=<>
- using System.Collections;
- public class XgCameraManager : MonoBehaviour
- {
- private string str_Control = "ScriptsControl";
- //public string str_OverAllControlName = "S1";
- //public string str_OptionalControlName = "S2";
- //public string str_CoreComControlName = "S3";
- //public string str_SellingPointControlName = "S4";
- [HideInInspector]
- public MainControl main_Control;
- //private S1_Control s1_Control;
- //private S2_Control s2_Control;
- //private S4_Control s4_Control;
- public void Init_CameraManager()
- {
- main_Control = GameObject.Find(str_Control).GetComponent
(); - //s1_Control = GameObject.Find(str_OverAllControlName).GetComponent
(); - //s2_Control = GameObject.Find(str_OptionalControlName).GetComponent
(); - //s4_Control = GameObject.Find(str_SellingPointControlName).GetComponent
(); - }
- public void OnManagerTap(TapGesture gesture)
- {
- if (gesture.Selection.layer == 10 && UICamera.isOverUI == false)
- {
- main_Control.MoveToPoint1(0);
- }
- if (gesture.Selection.layer == 11 && UICamera.isOverUI == false)
- {
- main_Control.MoveToPoint1(1);
- }
- if (gesture.Selection.layer == 12 && UICamera.isOverUI == false)
- {
- main_Control.MoveToPoint1(2);
- }
- if (gesture.Selection.layer == 13 && UICamera.isOverUI == false)
- {
- main_Control.MoveToPoint1(3);
- }
- if (gesture.Selection.layer == 14 && UICamera.isOverUI == false)
- {
- main_Control.MoveToPoint1(4);
- }
- //if (gesture.Selection.layer == 15 && UICamera.isOverUI == false)
- //{
- // main_Control.MoveToPoint1(5);
- //}
- if (gesture.Selection.layer == 16 && UICamera.isOverUI == false)
- {
- main_Control.MoveToPoint1(5);
- }
- if (gesture.Selection.layer == 17 && UICamera.isOverUI == false)
- {
- main_Control.MoveToPoint1(6);
- }
- if (gesture.Selection.layer == 18 && UICamera.isOverUI == false)
- {
- main_Control.HotPointAnim(0);
- }
- if (gesture.Selection.layer == 19 && UICamera.isOverUI == false)
- {
- main_Control.HotPointAnim(1);
- }
- if (gesture.Selection.layer == 20 && UICamera.isOverUI == false)
- {
- main_Control.HotPointAnim(2);
- }
- }
- }