Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Rotator : MonoBehaviour
  6. {
  7. private float maxRotationPerSec = 180f;
  8. protected Vector3 _screenCenter;
  9. [SerializeField]
  10. float rotation_limit_x;
  11. [SerializeField]
  12. float rotation_limit_y;
  13.  
  14. void Start()
  15. {
  16. _screenCenter = new Vector3(
  17. Screen.width,
  18. Screen.height,
  19. 0
  20. )/2;
  21.  
  22. /*
  23. _screenCenter = Camera
  24. .current
  25. .WorldToScreenPoint(
  26. transform.position
  27. );
  28. */
  29. Debug.Log(_screenCenter);
  30. }
  31.  
  32. // Update is called once per frame
  33. void Update()
  34. {
  35. #if UNITY_EDITOR
  36. if (!Input.GetMouseButton(0)) {
  37. return;
  38. }
  39. #endif
  40.  
  41. #if !UNITY_EDITOR
  42. if (Input.touchCount==0) {
  43. return;
  44. }
  45. #endif
  46.  
  47.  
  48. float xc = rotation_limit_x/_screenCenter.x;
  49. float xdistance = Input.mousePosition.x - _screenCenter.x;
  50. float xrotation = -xdistance * xc;
  51.  
  52. float yc = rotation_limit_y/_screenCenter.y;
  53. float ydistance = Input.mousePosition.y - _screenCenter.y;
  54. float yrotation = ydistance * yc;
  55. //float yrotation = 0f;
  56.  
  57. var tragetRotatin = new Vector3(yrotation,0,xrotation);//, maxRotationPerSec);
  58. Vector3 distance = tragetRotatin - transform.eulerAngles;
  59. var d2 = Vector3.Lerp(transform.eulerAngles, tragetRotatin, 0.8f);
  60.  
  61. var q = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(tragetRotatin), maxRotationPerSec * Time.deltaTime);
  62.  
  63. //Debug.Log("hw = "+hw+ " coef = "+c + " distance = "+distance + " rotation = "+rotation);
  64. transform.rotation = q;//new Vector3(yrotation,0,xrotation);
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement