Drag and Rotate object script for Unity in C/#


SUBMITTED BY: Guest

DATE: May 2, 2014, 12:33 p.m.

FORMAT: C#

SIZE: 1.5 kB

HITS: 849

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

comments powered by Disqus