Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class ItemRotator : MonoBehaviour {
- //чувствительность ввода
- public float sensivity = 10f;
- //скорость поворота. По-умолчанию 60 чтобы успеть повернуться к цели за 1/60 секунды.
- public float speed = 60f;
- float horizontalInput, verticalInput;
- //целевой поворот
- Quaternion targetRotation;
- Transform camTransform;
- private void Awake()
- {
- targetRotation = transform.rotation;
- }
- // Update is called once per frame
- void Update () {
- camTransform = Camera.main.transform;
- if (Input.GetMouseButton(0))
- {
- //обнуляем целевой поворот
- horizontalInput = 0f;
- verticalInput = 0f;
- }
- if (Input.GetMouseButton(0))
- {
- horizontalInput += Input.GetAxis("Mouse X") * sensivity;
- verticalInput += Input.GetAxis("Mouse Y") * sensivity;
- //повернем камеру вокруг оси вправо на verticalInput и вокруг оси вверх на -horizontalInput;
- Quaternion cameraSpaceRotation = Quaternion.Euler(camTransform.right * verticalInput) * Quaternion.Euler(camTransform.up * (-horizontalInput) ) * camTransform.rotation;
- //а теперь получим глобальный поворот объекта из нового поворота камеры и относительного поворота объекта к камере в начале вращения
- targetRotation = cameraSpaceRotation * Quaternion.Inverse(camTransform.rotation) * transform.rotation;
- }
- //устремляем поворот объекта к целевому повороту с заданной скоростью
- transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * speed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement