Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Rotator : MonoBehaviour
- {
- private float maxRotationPerSec = 180f;
- protected Vector3 _screenCenter;
- [SerializeField]
- float rotation_limit_x;
- [SerializeField]
- float rotation_limit_y;
- void Start()
- {
- _screenCenter = new Vector3(
- Screen.width,
- Screen.height,
- 0
- )/2;
- /*
- _screenCenter = Camera
- .current
- .WorldToScreenPoint(
- transform.position
- );
- */
- Debug.Log(_screenCenter);
- }
- // Update is called once per frame
- void Update()
- {
- #if UNITY_EDITOR
- if (!Input.GetMouseButton(0)) {
- return;
- }
- #endif
- #if !UNITY_EDITOR
- if (Input.touchCount==0) {
- return;
- }
- #endif
- float xc = rotation_limit_x/_screenCenter.x;
- float xdistance = Input.mousePosition.x - _screenCenter.x;
- float xrotation = -xdistance * xc;
- float yc = rotation_limit_y/_screenCenter.y;
- float ydistance = Input.mousePosition.y - _screenCenter.y;
- float yrotation = ydistance * yc;
- //float yrotation = 0f;
- var tragetRotatin = new Vector3(yrotation,0,xrotation);//, maxRotationPerSec);
- Vector3 distance = tragetRotatin - transform.eulerAngles;
- var d2 = Vector3.Lerp(transform.eulerAngles, tragetRotatin, 0.8f);
- var q = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(tragetRotatin), maxRotationPerSec * Time.deltaTime);
- //Debug.Log("hw = "+hw+ " coef = "+c + " distance = "+distance + " rotation = "+rotation);
- transform.rotation = q;//new Vector3(yrotation,0,xrotation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement