标签:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
/// <summary>
/// 主界面场景选择切换
///
/// 触摸让物体跟随手指手势转动,触摸停止后,物体都居中显示在屏幕上,3秒后让物体自转
///
/// </summary>
public class ZYDoHasRotateTheSphere : MonoBehaviour {
public GameObject Obj;//旋转的物体(球)
Vector3 touchPos; //触摸的位置
public float time = 3;
float timer = 0;
bool isRotate = true;
float lastTime;
void Update()
{
if (isRotate==true)
{
RotateIt();
Debug.Log("++++++++++++++++++++++++++++++has rotateIt?????"+ isRotate);
}
TouchRotate();//调用触摸的方法
}
#region 转换z轴的值,让物体每次都居中显示在屏幕上
void TransformTheAngle()
{
Debug.Log("值" + Obj.transform.localEulerAngles);
Obj.transform.localEulerAngles = new Vector3(0, 0, GetrotationValue(Obj.transform.localEulerAngles.z));//转换z的角度值
}
#endregion
#region 切换场景
//实现场景转换
public void OnMOFAKETANGClick()
{
Application.LoadLevel("MOFAKETANG");//切换到MOFAKETANG场景
}
public void OnSHENQIDESHIJIEClick()
{
Application.LoadLevel("SHENQIDESHIJIE");//切换到SHENQIDESHIJIE场景
}
public void OnWOAIJIDANCIClick()
{
Application.LoadLevel("WOAIJIDANCI");//切换到WOAIJIDANCI场景
}
public void OnBAOBAOQUANClick()
{
Application.LoadLevel("BAOBAOQUAN");//切换到BAOBAOQUAN场景
}
#endregion
#region 触摸让物体跟随旋转,停止触摸时,3秒后物体自转
//触摸旋转
void TouchRotate()
{
if (Input.touchCount == 1)
{//判断是否是一个手指在触摸
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{//判断是否在触摸移动
touchPos = Input.GetTouch(0).deltaPosition;//为触摸的初始位置初始化
Obj.transform.Rotate(0, 0, -touchPos.x * 3, Space.Self);//触摸让物体绕Z轴自转
isRotate = false; //停止自转
}
}
if(Input.GetTouch(0).phase==TouchPhase.Ended)
{ //判断是否触摸停止
TransformTheAngle();//转换角度
StartCoroutine(WaitDelaySeconds());//3秒后自转
}
}
#endregion
#region 更新z的值
//传入旋转的z值
private float GetrotationValue(float zValue)
{
float curValue = 0f;//物体与最近轴的角度差值
float value = 0f;//最近轴的角度值
if (zValue > 270)
{
curValue = zValue - 270;
value = 270;
}
else if (zValue > 180)
{
curValue = zValue - 180;
value = 180;
}
else if (zValue > 90)
{
curValue = zValue - 90;
value = 90;
}
else
{
curValue = zValue;
value = 0;
}
curValue = value + (GetValue(curValue));//计算差值
return curValue;//返回差值
}
#endregion
#region 得到每次物体与周围的两个轴向的最近值
//得到差值
private float GetValue(float value)
{
float curValue = 0;
if (value > 45)
{//判断大于45度,差值等于90度
curValue = 90;
}
else
{//小于则是0度
curValue = 0;
}
return curValue;
}
#endregion
#region 球自转
void RotateIt()
{//球自转
Obj.transform.Rotate(0, 0, 0.05f);
}
#endregion
#region 延迟3秒球自转
IEnumerator WaitDelaySeconds()
{//协程,延迟3秒后球自转
yield return new WaitForSeconds(3);
isRotate = true;
}
#endregion
}
标签:
原文地址:http://www.cnblogs.com/ZeroMurder/p/5664386.html