Spin object with mouse script for Unity (C#)


SUBMITTED BY: KaiMolan

DATE: Aug. 21, 2015, 8:55 p.m.

FORMAT: C#

SIZE: 1.5 kB

HITS: 514

  1. /*
  2. * Found this script useful? Please donate ^^
  3. * Bitcoin Address: 1LdyAQHXdFVqzGzah45sGKhsQ8wDE6jyci
  4. * Good luck and enjoy.
  5. *
  6. */
  7. using UnityEngine;
  8. namespace Assets.Scripts
  9. {
  10. public class DragRotateSlowDown :MonoBehaviour
  11. {
  12. private const float ROTATION_SPEED = 10.0f;
  13. private const float LERP_SPEED = 1.0f;
  14. private Vector3 _theSpeed;
  15. private Vector3 _avgSpeed;
  16. private bool _isDragging = false;
  17. private Vector3 _targetSpeedX;
  18. public void OnMouseDown()
  19. {
  20. _isDragging = true;
  21. }
  22. public void Update()
  23. {
  24. if (Input.GetMouseButton(0) && _isDragging)
  25. {
  26. _theSpeed = new Vector3(-Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0.0F);
  27. _avgSpeed = Vector3.Lerp(_avgSpeed, _theSpeed, Time.deltaTime * 5);
  28. }
  29. else
  30. {
  31. if (_isDragging)
  32. {
  33. _theSpeed = _avgSpeed;
  34. _isDragging = false;
  35. }
  36. var i = Time.deltaTime*LERP_SPEED;
  37. _theSpeed = Vector3.Lerp(_theSpeed, Vector3.zero, i);
  38. }
  39. transform.Rotate(Camera.main.transform.up * _theSpeed.x * ROTATION_SPEED, Space.World);
  40. transform.Rotate(Camera.main.transform.right * _theSpeed.y * ROTATION_SPEED, Space.World);
  41. }
  42. }
  43. }

comments powered by Disqus