TouchManipulator.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using UnityEngine;
  2. using System.Collections;
  3. [AddComponentMenu("Cocuy/Touch Manipulator")]
  4. public class TouchManipulator : MonoBehaviour {
  5. private Vector3 m_previousMousePosition;
  6. public float m_velocityStrength = 10f;
  7. public float m_velocityRadius = 5f;
  8. public float m_particlesStrength = 1f;
  9. public float m_particlesRadius = 5f;
  10. public FluidSimulator m_fluid;
  11. public ParticlesArea m_particlesArea;
  12. void LateUpdate()
  13. {
  14. for (int i = 0; i < Input.touchCount; ++i)
  15. {
  16. Touch touch = Input.GetTouch(i);
  17. Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0f));
  18. RaycastHit hitInfo = new RaycastHit();
  19. if (m_particlesArea.GetComponent<Collider>().Raycast(ray, out hitInfo, 100))
  20. {
  21. float fWidth = m_particlesArea.GetComponent<Renderer>().bounds.extents.x * 2f;
  22. float fRadius = (m_particlesRadius * m_particlesArea.GetWidth()) / fWidth;
  23. m_particlesArea.AddParticles(hitInfo.textureCoord, fRadius, m_particlesStrength * Time.deltaTime);
  24. if (touch.phase == TouchPhase.Moved)
  25. {
  26. Vector3 direction = new Vector3(touch.deltaPosition.x, touch.deltaPosition.y) * m_velocityStrength * touch.deltaTime;
  27. fWidth = m_fluid.GetComponent<Renderer>().bounds.extents.x * 2f;
  28. fRadius = (m_velocityRadius * m_fluid.GetWidth()) / fWidth;
  29. m_fluid.AddVelocity(hitInfo.textureCoord, direction, fRadius);
  30. }
  31. }
  32. }
  33. }
  34. }