一、Unity资料

1、Unity 手指滑动让物体旋转

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轴负方向移动=====");  
  
                backValue = -1; //沿着X轴负方向移动  
  
            } else {  
  
                //Debug.Log("=======沿着X轴正方向移动=====");  
  
                backValue = 1; //沿着X轴正方向移动  
  
            }  
  
        } else {  
  
            if (nowFingerPos.y - startFingerPos.y > 0) {  
  
                //Debug.Log("=======沿着Y轴正方向移动=====");  
  
                backValue = 2; //沿着Y轴正方向移动  
  
            } else {  
  
                //Debug.Log("=======沿着Y轴负方向移动=====");  
  
                backValue = -2; //沿着Y轴负方向移动  
  
            }  
  
        }  
        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、如何让物体旋转到指定角度

https://zhidao.baidu.com/question/2057667998339043267.html

例如:

如题,我想做两个按键,让一个模型能够绕一个轴正负方向旋转,按其中一个按键一次就旋转30度,再按一次这个按键就再旋转30度,也就是按4次就能-60度到60度.现在只能实现转一次,而且按钮的位置也不会改,求大神帮忙看看,最好告诉我用什么语句,提示提示思路啥的也行。我用itween插件,虽然能设置动画和指定角度,但是不会写按键触发,也只能旋转一次。宣雨松那个教程全是用JS写的,
我都用的C#...
现有的代码贴下..

using UnityEngine;
using System.Collections;

public class rotate : MonoBehaviour {       
 //碰撞的游戏对象        
private CharacterController controller = null;        
//旋转速度,暂时不用..
private float rotateSpeed = 5.0f;                                
void start()       
 {   
//获取角色控制器对象                
controller = GetComponent();       
 }        
void OnGUI()        
{       
//暂无法控制此按键在游戏中位置?!              
  if(GUILayout.RepeatButton("向右旋转"))                
{   
//绕Y轴旋转                        
//暂时不用
transform.Rotate(0,-rotateSpeed,0);                        
this.transform.rotation = Quaternion.Euler(0,0,-30);               
 }                
if(GUILayout.RepeatButton("向左旋转"))               
 {                        
//暂时不用
transform.Rotate(0,rotateSpeed,0);                        
this.transform.rotation = Quaternion.Euler(0,0,30);                
            }       
      }
}

你可能感兴趣的:(一、Unity资料)