1 using UnityEngine;
2
3 public
class RotateCamera : MonoBehaviour
4 {
5
6     
//Can Use Touch
7     
bool isTouchDevice;
8
9     
//Speed
10     
float arrowMouseSpeed = 1.0f;
11
12     
// Use this for initialization
13     
void Start()
14     {
15         
//check if touch device
16         isTouchDevice = CanUseTouch.canUseTouchM();
17
18         
//Get local rotation
19         Vector3 rot = transform.localRotation.eulerAngles;
20         rotY = rot.y;
21         rotX = rot.x;
22
23     }
24
25     
// Update is called once per frame
26     
void Update()
27     {
28         
if (isTouchDevice)
29         {
30             
//Rotate Camera with Accelerometer
31             cameraRotationAccelerometer();
32         }
33         
else
34         {
35             
//Rotate Camera with Keyboard Arrow and Mouse
36             moveWithArrowAndMouse();
37         }
38     }

39
40     ///
//////////**** Accelerometer Start****////////////////////////////////////////////////////////
41     
float xValue;
42     
float xValueMinMax = 5.0f;
43
44     
float cameraSpeed = 20.0f;// Greater the lower speed
45     Vector3 accelometerSmoothValue;
46
47     
void cameraRotationAccelerometer()
48     {
49         
//Set X Min Max
50         
if (xValue < -xValueMinMax)
51             xValue = -xValueMinMax;
52
53         
if (xValue > xValueMinMax)
54             xValue = xValueMinMax;
55
56         accelometerSmoothValue = lowpass();
57
58         xValue += accelometerSmoothValue.x;
59
60         transform.rotation =
new Quaternion(0, xValue, 0, cameraSpeed);
61     }
62
63     
//Smooth Accelerometer
64     
public float AccelerometerUpdateInterval = 1.0f / 100.0f;
65     
public float LowPassKernelWidthInSeconds = 0.001f;
66     
public Vector3 lowPassValue = Vector3.zero;
67     Vector3 lowpass()
68     {
69         
float LowPassFilterFactor = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds;//tweakable
70         lowPassValue = Vector3.Lerp(lowPassValue, Input.acceleration, LowPassFilterFactor);
71         
return lowPassValue;
72     }

73     ///
//////////**** Accelerometer End****////////////////////////////////////////////////////////
74
75     
//Move with Keyboard Arrow
76     
void moveWithArrowAndMouse()
77     {
78         
//Keyboard Arrow
79         moveCamera(Input.GetAxis(
"Horizontal"), Input.GetAxis("Vertical"), arrowMouseSpeed);
80
81         
//Keyboard Mouse
82         moveCamera(Input.GetAxis(
"Mouse X"), Input.GetAxis("Mouse Y"), arrowMouseSpeed);
83     }
84
85     
//Move Parameters
86     
float mouseX;
87     
float mouseY;
88     Quaternion localRotation;
89     
private float rotY = 0.0f; // rotation around the up/y axis
90     
private float rotX = 0.0f; // rotation around the right/x axis
91
92     
void moveCamera(float horizontal, float verticle, float moveSpeed)
93     {
94         mouseX = horizontal;
95         mouseY = -verticle;
96
97         rotY += mouseX * moveSpeed;
98         rotX += mouseY * moveSpeed;
99
100         localRotation = Quaternion.Euler(rotX, rotY,
0.0f);
101         transform.rotation = localRotation;
102     }
103 }


Can Use Touch

Speed

Use this for initialization

check if touch device

Get local rotation

Update is called once per frame

Rotate Camera with Accelerometer

Rotate Camera with Keyboard Arrow and Mouse

**** Accelerometer Start****

float cameraSpeed = 20.0f; Greater the lower speed

Set X Min Max

Smooth Accelerometer

float LowPassFilterFactor = AccelerometerUpdateInterval LowPassKernelWidthInSeconds;tweakable

**** Accelerometer End****

Move with Keyboard Arrow

Keyboard Arrow

Keyboard Mouse

Move Parameters

private float rotY = 0.0f; rotation around the upy axis

private float rotX = 0.0f; rotation around the rightx axis



Gõ tìm kiếm nhanh...