第一次写博客,刚开始学习unity。把近期所学记录下来~
最近做的demo是控制一个摄像头在一个室内的场景中自由移动,类似于第一人称视角。
1.用鼠标和键盘控制
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public float near = 20.0f;
public float far = 100.0f;
public float sensitivetyZ = 2f;
public float sensitivityX = 10f;
public float sensitivityY = 10f;
public float sensitivetyMove = 2f;
public float sensitivetyMouseWheel = 2f;
void Update()
{
// 滚轮实现镜头缩进和拉远
//鼠标右键实现视角转动,类似第一人称视角
if (Input.GetMouseButton(1))
{
float rotationX = Input.GetAxis("Mouse X") * sensitivityX;
float rotationY = Input.GetAxis("Mouse Y") * sensitivityY;
transform.Rotate(-rotationY, rotationX, 0);
}
//键盘按钮←和→实现视角水平旋转
if (Input.GetAxis("Horizontal") != 0)
{
float rotationZ = Input.GetAxis("Horizontal") * sensitivetyZ;
transform.Rotate(0, 0, rotationZ);
}
}
}
2.在手机中触摸控制摄像头
using UnityEngine;
using System.Collections;
public class cameracoltroller : MonoBehaviour {
public float sensitivityX = 0.0001f;
public float sensitivityY = 0.0001f;
void update()
{
<span style="white-space:pre"> </span>//单指控制摄像头旋转
if(Input.touchCount==1)
{
float rotationX = Input.GetAxis("Mouse X") * sensitivityX;
float rotationY = Input.GetAxis("Mouse Y") * sensitivityY;
transform.Rotate(-rotationY, rotationX, 0);
}
<span style="white-space:pre"> </span>//双指控制摄像头移动
if(Input.touchCount==2)
{
transform.position += transform.forward * Time.deltaTime * 20;
}
}
}
3.通过手机陀螺仪控制摄像头旋转
using UnityEngine;
using System.Collections;
public class cameracontroller : MonoBehaviour
{
#region [Private fields]
private bool gyroEnabled = true;
private const float lowPassFilterFactor = 0.1f;
public bool gyro = true;
private const float speed = 0.2f;
//private Quaternion previousAttitude;
private readonly Quaternion baseIdentity = Quaternion.Euler(90, 0, 0);
private readonly Quaternion landscapeRight = Quaternion.Euler(0, 0, 90);
private readonly Quaternion landscapeLeft = Quaternion.Euler(0, 0, -90);
private readonly Quaternion upsideDown = Quaternion.Euler(0, 0, 180);
private Quaternion cameraBase = Quaternion.identity;
private Quaternion calibration = Quaternion.identity;
private Quaternion baseOrientation = Quaternion.Euler(90, 0, 0);
private Quaternion baseOrientationRotationFix = Quaternion.identity;
private Quaternion startrotation = Quaternion.identity;
private Quaternion referanceRotation = Quaternion.identity;
private bool debug = true;
#endregion
#region [Unity events]
protected void Start()
{
AttachGyro();
Input.gyro.enabled = true;
Input.gyro.updateInterval = 0.05f;
startrotation =Quaternion.Inverse(Input.gyro.attitude);
}
void Update()
{
<span style="white-space:pre"> </span>//退出
if(Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
if (Input.gyro.enabled)
{
//摄像头旋转
transform.rotation = Quaternion.Slerp(transform.rotation,
cameraBase * (ConvertRotation(referanceRotation * Input.gyro.attitude) * GetRotFix()), lowPassFilterFactor);
}
}
protected void OnGUI()
{
if (!debug)
return;
GUILayout.Label("Orientation: " + Screen.orientation, GUILayout.Height(200), GUILayout.Width(200));
GUILayout.Label("Calibration: " + calibration);
GUILayout.Label("Camera base: " + cameraBase);
GUILayout.Label("input.gyro.attitude: " + Input.gyro.attitude);
GUILayout.Label("transform.rotation: " + transform.rotation);
GUILayout.Label("startroattitude:" + startrotation);
GUI.skin.label.fontSize = 50;
GUI.skin.button.fontSize = 100;
if (GUILayout.Button("On/off gyro: " + gyro))
{
Input.gyro.enabled = !Input.gyro.enabled;
}
if (GUILayout.Button("On/off gyro controller: " + gyroEnabled, GUILayout.Height(100)))
{
if (gyroEnabled)
{
DetachGyro();
}
else
{
AttachGyro();
}
}
if (GUILayout.Button("Update gyro calibration (Horizontal only)", GUILayout.Height(80)))
{
UpdateCalibration(true);
}
if (GUILayout.Button("Update camera base rotation (Horizontal only)", GUILayout.Height(80)))
{
UpdateCameraBaseRotation(true);
}
if (GUILayout.Button("Reset base orientation", GUILayout.Height(80)))
{
ResetBaseOrientation();
}
if (GUILayout.Button("Reset camera rotation", GUILayout.Height(80)))
{
transform.rotation = Quaternion.identity;
}
}
#endregion
#region [Public methods]
/// <summary>
/// Attaches gyro controller to the transform.
/// </summary>
private void AttachGyro()
{
gyroEnabled = true;
ResetBaseOrientation();
UpdateCalibration(true);
UpdateCameraBaseRotation(true);
RecalculateReferenceRotation();
}
/// <summary>
/// Detaches gyro controller from the transform
/// </summary>
private void DetachGyro()
{
gyroEnabled = false;
}
#endregion
#region [Private methods]
/// <summary>
/// Update the gyro calibration.
/// </summary>
private void UpdateCalibration(bool onlyHorizontal)
{
if (onlyHorizontal)
{
var fw = (Input.gyro.attitude) * (-Vector3.forward);
fw.z = 0;
if (fw == Vector3.zero)
{
calibration = Quaternion.identity;
}
else
{
calibration = (Quaternion.FromToRotation(baseOrientationRotationFix * Vector3.up, fw));
}
}
else
{
calibration = Input.gyro.attitude;
}
}
/// <summary>
/// Update the camera base rotation.
/// </summary>
/// <param name='onlyHorizontal'>
/// Only y rotation.
/// </param>
private void UpdateCameraBaseRotation(bool onlyHorizontal)
{
if (onlyHorizontal)
{
var fw = transform.forward;
fw.y = 0;
if (fw == Vector3.zero)
{
cameraBase = Quaternion.identity;
}
else
{
cameraBase = Quaternion.FromToRotation(Vector3.forward, fw);
}
}
else
{
cameraBase = transform.rotation;
}
}
/// <summary>
/// Converts the rotation from right handed to left handed.
/// </summary>
/// <returns>
/// The result rotation.
/// </returns>
/// <param name='q'>
/// The rotation to convert.
/// </param>
private static Quaternion ConvertRotation(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
/// <summary>
/// Gets the rot fix for different orientations.
/// </summary>
/// <returns>
/// The rot fix.
/// </returns>
private Quaternion GetRotFix()
{
#if UNITY_3_5
if (Screen.orientation == ScreenOrientation.Portrait)
return Quaternion.identity;
if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.Landscape)
return landscapeLeft;
if (Screen.orientation == ScreenOrientation.LandscapeRight)
return landscapeRight;
if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
return upsideDown;
return Quaternion.identity;
#else
return Quaternion.identity;
#endif
}
/// <summary>
/// Recalculates reference system.
/// </summary>
private void ResetBaseOrientation()
{
baseOrientationRotationFix = GetRotFix();
baseOrientation = baseOrientationRotationFix * baseIdentity;
}
/// <summary>
/// Recalculates reference rotation.
/// </summary>
private void RecalculateReferenceRotation()
{
referanceRotation = Quaternion.Inverse(baseOrientation) * Quaternion.Inverse(calibration);
}
#endregion
}