Advertisement
dnnkeeper

ItemRotator

Feb 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class ItemRotator : MonoBehaviour {
  4.  
  5.     //чувствительность ввода
  6.     public float sensivity = 10f;
  7.  
  8.     //скорость поворота. По-умолчанию 60 чтобы успеть повернуться к цели за 1/60 секунды.
  9.     public float speed = 60f;
  10.  
  11.     float horizontalInput, verticalInput;
  12.  
  13.     //целевой поворот
  14.     Quaternion targetRotation;
  15.  
  16.     Transform camTransform;
  17.  
  18.     private void Awake()
  19.     {
  20.         targetRotation = transform.rotation;
  21.     }
  22.  
  23.     // Update is called once per frame
  24.     void Update () {
  25.  
  26.         camTransform = Camera.main.transform;
  27.  
  28.         if (Input.GetMouseButton(0))
  29.         {
  30.             //обнуляем целевой поворот
  31.            
  32.             horizontalInput = 0f;
  33.  
  34.             verticalInput = 0f;
  35.         }
  36.  
  37.         if (Input.GetMouseButton(0))
  38.         {
  39.             horizontalInput += Input.GetAxis("Mouse X") * sensivity;
  40.             verticalInput += Input.GetAxis("Mouse Y") * sensivity;
  41.  
  42.             //повернем камеру вокруг оси вправо на verticalInput и вокруг оси вверх на -horizontalInput;
  43.             Quaternion cameraSpaceRotation = Quaternion.Euler(camTransform.right * verticalInput) * Quaternion.Euler(camTransform.up * (-horizontalInput) ) * camTransform.rotation;
  44.  
  45.             //а теперь получим глобальный поворот объекта из нового поворота камеры и относительного поворота объекта к камере в начале вращения
  46.             targetRotation = cameraSpaceRotation * Quaternion.Inverse(camTransform.rotation) * transform.rotation;
  47.         }
  48.  
  49.         //устремляем поворот объекта к целевому повороту с заданной скоростью
  50.         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * speed);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement