当前位置:   article > 正文

Unity触摸激活旋转缩放_scale=vector3.one*mathf.clamp(scale.x,0.3f,3f);

scale=vector3.one*mathf.clamp(scale.x,0.3f,3f);

RotateAndZoom

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Assertions.Comparers;
  5. public class RotateAndZoom : MonoBehaviour
  6. {
  7. public int id;
  8. public bool isMain;
  9. public static RotateAndZoom _instance;
  10. private Quaternion startQuaternion;
  11. private Vector3 startScale;
  12. private Vector3 startPos;
  13. private Vector3 centerPos;
  14. private Touch frisTouch;
  15. private Touch secondTouch;
  16. private bool autoRotate;
  17. private bool isRecover;
  18. [HideInInspector]
  19. public float coolTime;
  20. private bool isCenter;
  21. private float waitTime;
  22. private float recoverTime;
  23. private float centerTime;
  24. private bool isTouch;
  25. private bool isCool;
  26. void Awake()
  27. {
  28. _instance = this;
  29. }
  30. void Start()
  31. {
  32. isCool = true;
  33. centerPos = new Vector3(0,0,-1);
  34. startQuaternion = transform.rotation;
  35. startScale = transform.localScale;
  36. startPos = transform.position;
  37. autoRotate = true;
  38. }
  39. void Update()
  40. {
  41. AutoRoattionAndRecover();
  42. if (Input.touchCount < 0)
  43. return;
  44. if (isTouch)
  45. {
  46. if (Input.touchCount > 0)
  47. {
  48. Touch touch = Input.GetTouch(0);
  49. Vector2 deltaPos = touch.deltaPosition;
  50. transform.Rotate(Vector3.down*deltaPos.x*2, Space.World);
  51. transform.Rotate(Vector3.right*deltaPos.y*2, Space.World);
  52. }
  53. if (Input.touchCount > 0)
  54. {
  55. Touch newTouch1 = Input.GetTouch(0);
  56. Touch newTouch2 = Input.GetTouch(1);
  57. if (newTouch2.phase == TouchPhase.Began)
  58. {
  59. frisTouch = newTouch1;
  60. secondTouch = newTouch2;
  61. return;
  62. }
  63. float startDistance = Vector2.Distance(frisTouch.position, secondTouch.position);
  64. float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
  65. float offset = newDistance - startDistance;
  66. float scaleFactor = offset/100f;
  67. Vector3 localScale = transform.localScale;
  68. Vector3 scale = new Vector3(localScale.x + scaleFactor, localScale.y + scaleFactor,
  69. localScale.z + scaleFactor);
  70. if (scale.x > 0.3f && scale.y > 0.3f && scale.z > 0.3f)
  71. {
  72. transform.localScale = scale;
  73. }
  74. float scale1 = Mathf.Clamp(transform.localScale.x, 3f, 4.5f);
  75. transform.localScale = Vector3.one*scale1;
  76. ;
  77. frisTouch = newTouch1;
  78. secondTouch = newTouch2;
  79. }
  80. }
  81. if (isMain && !isRecover)
  82. {
  83. if (Input.touchCount > 0)
  84. {
  85. if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Moved)
  86. {
  87. coolTime = 0;
  88. }
  89. }
  90. if (isCool)
  91. isCenter = true;
  92. autoRotate = false;
  93. waitTime += Time.deltaTime;
  94. if (waitTime >= 2)
  95. {
  96. isTouch = true;
  97. }
  98. coolTime += Time.deltaTime;
  99. if (coolTime >= 3)
  100. {
  101. coolTime = 0;
  102. isRecover = true;
  103. isTouch = false;
  104. waitTime = 0;
  105. isMain = false;
  106. isCenter = false;
  107. }
  108. }
  109. }
  110. void AutoRoattionAndRecover()
  111. {
  112. if (autoRotate)
  113. transform.RotateAround(transform.position, Vector3.up, 30 * Time.deltaTime);
  114. if (isRecover)
  115. {
  116. transform.position = Vector3.Lerp(transform.position, startPos, 2*Time.deltaTime);
  117. transform.rotation = Quaternion.Lerp(transform.rotation, startQuaternion, 2*Time.deltaTime);
  118. transform.localScale = Vector3.Lerp(transform.localScale, startScale, 2*Time.deltaTime);
  119. recoverTime += Time.deltaTime;
  120. if (recoverTime >= 2)
  121. {
  122. recoverTime = 0;
  123. autoRotate = true;
  124. isRecover = false;
  125. isCool = true;
  126. }
  127. }
  128. if (isCenter)
  129. {
  130. transform.position = Vector3.Lerp(transform.position, centerPos, 2*Time.deltaTime);
  131. transform.localScale = Vector3.Lerp(transform.localScale, Vector3.one*2.8f, 2*Time.deltaTime);
  132. centerTime += Time.deltaTime;
  133. if (centerTime >= 2)
  134. {
  135. centerTime = 0;
  136. isCool = false;
  137. isCenter = false;
  138. }
  139. }
  140. }
  141. }

GameManager

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Security.Policy;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class GameManager : MonoBehaviour
  7. {
  8. public static GameManager _instance;
  9. public Transform[] spawnTargetpos;
  10. public GameObject targetGo;
  11. public Transform parent;
  12. public Text getText;
  13. [HideInInspector]
  14. public List<GameObject> goList = new List<GameObject>();
  15. private int touchIndex = -1;
  16. private bool isStop;
  17. private RaycastHit hitInfor;
  18. private RotateAndZoom touchGo_RotateZoom ;
  19. void Awake()
  20. {
  21. _instance = this;
  22. }
  23. void Start()
  24. {
  25. isStop = true;
  26. }
  27. void Update()
  28. {
  29. if (Input.touchCount > 0)
  30. {
  31. if (Input.GetTouch(0).phase == TouchPhase.Began)
  32. {
  33. LoopAddGameObject();
  34. touchIndex++;
  35. GameObject go = GameObject.Instantiate(targetGo) as GameObject;
  36. go.transform.position = spawnTargetpos[touchIndex].position;
  37. RotateAndZoom rotateAndZoom = go.GetComponent<RotateAndZoom>();
  38. rotateAndZoom.id = touchIndex;
  39. goList.Add(go);
  40. Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
  41. Physics.Raycast(ray, out hitInfor, 100);
  42. if (!hitInfor.collider)
  43. {
  44. }
  45. else
  46. {
  47. touchGo_RotateZoom = hitInfor.collider.gameObject.GetComponent<RotateAndZoom>();
  48. touchGo_RotateZoom.isMain = true;
  49. touchGo_RotateZoom.coolTime = 0;
  50. HeadOtherCollider(touchGo_RotateZoom.id);
  51. getText.text = touchGo_RotateZoom.id.ToString();
  52. }
  53. }
  54. }
  55. CheckTouchObjectState();
  56. }
  57. void HeadOtherCollider(int id)
  58. {
  59. for (int i = 0; i < goList.Count; i++)
  60. {
  61. RotateAndZoom rotateAndZoom = goList[i].GetComponent<RotateAndZoom>();
  62. if (rotateAndZoom.id == id)
  63. {
  64. Collider collider = rotateAndZoom.GetComponent<Collider>();
  65. //MeshRenderer renderer = rotateAndZoom.GetComponent<MeshRenderer>();
  66. collider.enabled = true;
  67. //renderer.material.color = Color.red;
  68. }
  69. else
  70. {
  71. Collider collider = rotateAndZoom.GetComponent<Collider>();
  72. //MeshRenderer renderer = rotateAndZoom.GetComponent<MeshRenderer>();
  73. collider.enabled = false;
  74. //renderer.material.color = Color.blue;
  75. }
  76. }
  77. }
  78. void CheckTouchObjectState()
  79. {
  80. if (touchGo_RotateZoom == null)
  81. {
  82. return;
  83. }
  84. if (!touchGo_RotateZoom.isMain)
  85. {
  86. for (int i = 0; i < goList.Count; i++)
  87. {
  88. Collider collider = goList[i].GetComponent<Collider>();
  89. //MeshRenderer renderer = goList[i].GetComponent<MeshRenderer>();
  90. collider.enabled = true;
  91. //renderer.material.color = Color.blue;
  92. }
  93. }
  94. }
  95. void LoopAddGameObject()
  96. {
  97. if (goList.Count > 10)
  98. {
  99. print(touchIndex);
  100. for (int i = 0; i < goList.Count; i++)
  101. {
  102. RotateAndZoom rotateAndZoom1 = goList[i].GetComponent<RotateAndZoom>();
  103. if (rotateAndZoom1.id == touchIndex - 10)
  104. {
  105. MeshRenderer[] renderers = rotateAndZoom1.GetComponentsInChildren<MeshRenderer>();
  106. for (int j = 0; j < renderers.Length; j++)
  107. {
  108. renderers[i].enabled = false;
  109. }
  110. goList.Remove(rotateAndZoom1.gameObject);
  111. GameObject.Destroy(rotateAndZoom1.gameObject, 0.5f);
  112. }
  113. }
  114. }
  115. }
  116. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/108171?site
推荐阅读
相关标签
  

闽ICP备14008679号