1缤削、Unity 手指滑動(dòng)讓物體旋轉(zhuǎn)
http://blog.csdn.net/ldy597321444/article/details/52859360
private Vector3 startFingerPos;
private Vector3 nowFingerPos;
private float xMoveDistance;
private float yMoveDistance;
private int backValue = 0;
public GameObject my_Cube;
public void JudgeFinger ()
{
//沒有觸摸
if (Input.touchCount <= 0) {
return;
}
if (Input.GetTouch (0).phase == TouchPhase.Began ) {
//Debug.Log("======開始觸摸=====");
startFingerPos = Input.GetTouch (0).position;
}
nowFingerPos = Input.GetTouch (0).position;
if ((Input.GetTouch (0).phase == TouchPhase.Stationary) || (Input.GetTouch(0).phase == TouchPhase.Ended)) {
startFingerPos = nowFingerPos;
//Debug.Log("======釋放觸摸=====");
return;
}
// if (Input.GetTouch(0).phase == TouchPhase.Ended) {
//
// }
if (startFingerPos == nowFingerPos) {
return;
}
xMoveDistance = Mathf.Abs (nowFingerPos.x - startFingerPos.x);
yMoveDistance = Mathf.Abs (nowFingerPos.y - startFingerPos.y);
if (xMoveDistance > yMoveDistance) {
if (nowFingerPos.x - startFingerPos.x > 0) {
//Debug.Log("=======沿著X軸負(fù)方向移動(dòng)=====");
backValue = -1; //沿著X軸負(fù)方向移動(dòng)
} else {
//Debug.Log("=======沿著X軸正方向移動(dòng)=====");
backValue = 1; //沿著X軸正方向移動(dòng)
}
} else {
if (nowFingerPos.y - startFingerPos.y > 0) {
//Debug.Log("=======沿著Y軸正方向移動(dòng)=====");
backValue = 2; //沿著Y軸正方向移動(dòng)
} else {
//Debug.Log("=======沿著Y軸負(fù)方向移動(dòng)=====");
backValue = -2; //沿著Y軸負(fù)方向移動(dòng)
}
}
if (backValue == -1) {
my_Cube.transform.Rotate (Vector3.back * Time.deltaTime * 300, Space.World);
my_Color.transform.Rotate (Vector3.back * Time.deltaTime * 300, Space.World);
} else if (backValue == 1) {
my_Cube.transform.Rotate (Vector3.back * -1 * Time.deltaTime * 300, Space.World);
my_Color.transform.Rotate (Vector3.back * -1 * Time.deltaTime * 300, Space.World);
}
// else if (backValue == 2) {
// my_Cube.transform.Rotate(Vector3.right * Time.deltaTime * 200 , Space.World);
// } else if (backValue == -2) {
// my_Cube.transform.Rotate(Vector3.right * -1 * Time.deltaTime * 200 , Space.World);
// }
}
2、如何讓物體旋轉(zhuǎn)到指定角度
https://zhidao.baidu.com/question/2057667998339043267.html
例如:
如題澄成,我想做兩個(gè)按鍵胧洒,讓一個(gè)模型能夠繞一個(gè)軸正負(fù)方向旋轉(zhuǎn)畏吓,按其中一個(gè)按鍵一次就旋轉(zhuǎn)30度,再按一次這個(gè)按鍵就再旋轉(zhuǎn)30度卫漫,也就是按4次就能-60度到60度.現(xiàn)在只能實(shí)現(xiàn)轉(zhuǎn)一次菲饼,而且按鈕的位置也不會(huì)改,求大神幫忙看看列赎,最好告訴我用什么語句宏悦,提示提示思路啥的也行。我用itween插件包吝,雖然能設(shè)置動(dòng)畫和指定角度饼煞,但是不會(huì)寫按鍵觸發(fā),也只能旋轉(zhuǎn)一次诗越。宣雨松那個(gè)教程全是用JS寫的砖瞧,
我都用的C#...
現(xiàn)有的代碼貼下..
using UnityEngine;
using System.Collections;
public class rotate : MonoBehaviour {
//碰撞的游戲?qū)ο?
private CharacterController controller = null;
//旋轉(zhuǎn)速度,暫時(shí)不用..
private float rotateSpeed = 5.0f;
void start()
{
//獲取角色控制器對(duì)象
controller = GetComponent<CharacterController>();
}
void OnGUI()
{
//暫無法控制此按鍵在游戲中位置嚷狞?块促!
if(GUILayout.RepeatButton("向右旋轉(zhuǎn)"))
{
//繞Y軸旋轉(zhuǎn)
//暫時(shí)不用
transform.Rotate(0,-rotateSpeed,0);
this.transform.rotation = Quaternion.Euler(0,0,-30);
}
if(GUILayout.RepeatButton("向左旋轉(zhuǎn)"))
{
//暫時(shí)不用
transform.Rotate(0,rotateSpeed,0);
this.transform.rotation = Quaternion.Euler(0,0,30);
}
}
}