Unity 收藏本版 已有9人收藏 +发表新主题
查看: 25929|回复: 0
打印 上一主题 下一主题

[分享] 总结unity中操作物体任意方向旋转的三种方法

[复制链接]

[分享] 总结unity中操作物体任意方向旋转的三种方法

yangyuxin 发表于 2016-4-13 23:23:41 浏览:  25929 回复:  0 只看该作者 复制链接
方法一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private Transform hitTransfrom;
           
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(mouseray, out hit))
        {
            hitTransfrom = hit.transform;
        }
    }
    else if (Input.GetMouseButtonUp(0))
    {
        hitTransfrom = null;
    }
    if (hitTransfrom)
    {
        Matrix4x4 localmatrix = hitTransfrom.worldToLocalMatrix;
        Vector3 vUp = localmatrix.MultiplyVector(new Vector3(0, 1, 0));
        Vector3 vRight = -localmatrix.MultiplyVector(new Vector3(1, 0, 0));
        float fMoveX = -Input.GetAxis("Mouse X") * Time.deltaTime * 200.0f;
        Quaternion rotation = Quaternion.AngleAxis(fMoveX, vUp);
        hitTransfrom.localRotation *= rotation;
        float fMoveY = -Input.GetAxis("Mouse Y") * Time.deltaTime * 200.0f;
        Quaternion rotoy = Quaternion.AngleAxis(fMoveY, vRight);
        hitTransfrom.localRotation *= rotoy;
    }
}



方法二:
1
2
3
4
5
6
7
8
9
10
11
using UnityEngine;
using System.Collections;
         
public class DragRotate : MonoBehaviour
{
    void OnMouseDrag ()
    {
        this.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"),-Input.GetAxis("Mouse X"),0)*6f,Space.World);
    }
         
}






方法三(带惯性的拖拽):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using UnityEngine;
using System.Collections;
     
public class DragRotate : MonoBehaviour
{
    private bool onDrag = false;
    public float speed = 6f;
    private float tempSpeed;
    private float axisX;
    private float axisY;
    void OnMouseDrag ()
    {
        onDrag = true;
        axisX=-Input.GetAxis ("Mouse X");
        axisY=Input.GetAxis ("Mouse Y");
    }
         
    float Rigid ()
    {
        if (onDrag) {
            if (tempSpeed < speed) {
                tempSpeed += speed * Time.deltaTime*5;
            }else{
                tempSpeed=speed;
            }
        } else {
            if(tempSpeed>0){
                tempSpeed-=speed * Time.deltaTime;
            }else{
                tempSpeed = 0;
            }
        }
        return tempSpeed;
    }
         
    void Update ()
    {
        this.transform.Rotate (new Vector3 (axisY, axisX, 0) * Rigid (), Space.World);
        if (!Input.GetMouseButton (0)) {
            onDrag = false;
        }
    }
}





另外入了缩放的代码,缩放、自动旋转、拖拽物体的综合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using UnityEngine;
using System.Collections;
  
  
/**
   
* @Function:使物体自动旋转,鼠标能够360°拖转物体,托转物体的时候,自动旋转停止,同时滚轮实现物体的缩放功能
* @Ahthor:黄杰
* @Date:2013-04-24
   
*/
public class ZoomAndDrag : MonoBehaviour {
  
  
    public Camera MainCamera;
    public float ZoomMin;      //滚轮的最小值
    public  float ZoomMax;      //滚轮的最大值
    private float normalDistance;   //设置摄像机的景深值
    private float MouseWheelSencitivity = 10.0f;    //鼠标灵敏度,就是缩放的速度的快慢
  
    private float axisX;
    private float axisY;
    public float speed = 6f;
    private float tempSpeed;
  
    private bool RoationOnly;
  
    void Start ()
    {
        normalDistance = 50.0f;
        ZoomMin = 20.0f;
        ZoomMax = 100.0f;
        RoationOnly = true;
    }
      
      
    void Update ()
    {
        Roation();
        Zoom();
        this.transform.Rotate(new Vector3(axisY, axisX, 0) * Rigid(), Space.World);     //物体旋转的方法
    }
  
  
    //自动旋转物体的方法,放在Update中调用
    void Roation()
    {
        if (RoationOnly)
        {
            gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 10);
        }
    }
  
    /****
    *鼠标滚轮缩放物体的方法
     *
     * **/
    void Zoom()
    {
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            if (normalDistance >= ZoomMin && normalDistance <= ZoomMax)
            {
                normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSencitivity;
            }
            if (normalDistance < ZoomMin)
            {
                normalDistance = ZoomMin;
            }
            if (normalDistance > ZoomMax)
            {
                normalDistance = ZoomMax;
            }
  
            MainCamera.fieldOfView = normalDistance;
        }
    }
  /***
   *
   * 鼠标左键控制物体360°旋转+惯性
   * **/
    float Rigid()
    {
        if (Input.GetMouseButton(0))
        {
            RoationOnly = false;    //当鼠标按下的时候,停止自动旋转
  
            axisX = Input.GetAxis("Mouse X");
            axisY = Input.GetAxis("Mouse Y");
            if (tempSpeed < speed)
            {
                tempSpeed += speed * Time.deltaTime * 5;
            }
            else
            {
                tempSpeed = speed;
            }
        }
        else
        {
            RoationOnly = true;     //当鼠标没有按下的时候,恢复自动旋转
            if (tempSpeed > 0)
            {
                tempSpeed -= speed * Time.deltaTime;
            }
            else
            {
                tempSpeed = 0;
            }
        }
        return tempSpeed;
    }
}




分享至:
| 人收藏
回复

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright © 2013-2017 ARinChina-增强现实中国技术论坛   All Rights Reserved.