回答編集履歴
4
Quaternion.LookRotation
answer
CHANGED
@@ -35,7 +35,7 @@
|
|
35
35
|
Vector3 delta=p-lastPos;
|
36
36
|
|
37
37
|
Vector3 dir=Camera.main.tranform.rotation*delta;
|
38
|
-
target.transform.rotation=
|
38
|
+
target.transform.rotation=Quaternion.LookRotation(dir);
|
39
39
|
lastPos=p;
|
40
40
|
}
|
41
41
|
}
|
@@ -56,7 +56,7 @@
|
|
56
56
|
Vector3 delta=p-lastPos;
|
57
57
|
|
58
58
|
Vector3 dir=Camera.main.tranform.rotation*delta;
|
59
|
-
q=
|
59
|
+
q=Quaternion.LookRotation(dir);
|
60
60
|
lastPos=p;
|
61
61
|
}
|
62
62
|
target.transform.rotation=Quarternion.Lerp(target.transform.rotation,q,Time.deltaTime);
|
3
追記
answer
CHANGED
@@ -15,14 +15,58 @@
|
|
15
15
|
Vector3 delta=p-lastPos;
|
16
16
|
Vector3 normal=Quarternion.Euler(0,0,90)*delta;
|
17
17
|
Vector3 axis=Camera.main.tranform.rotation*normal;
|
18
|
-
|
18
|
+
target.transform.rotation*=Quarternion.AngleAxis(delta.magnitude,axis.normalized);
|
19
19
|
lastPos=p;
|
20
20
|
}
|
21
21
|
}
|
22
22
|
}
|
23
23
|
```
|
24
|
+
向かせるプログラム
|
25
|
+
```c#
|
26
|
+
public class SwipeLookAt:MonoBehavior{
|
27
|
+
public GameObject target;
|
28
|
+
Vector3 lastPos;
|
29
|
+
void Update(){
|
30
|
+
p=Input.mousePosition;
|
31
|
+
if(Input.GetMouseButtonDown(0)){
|
32
|
+
lastPos=p;
|
33
|
+
}
|
34
|
+
else if(Input.GetMouseButton(0)){
|
35
|
+
Vector3 delta=p-lastPos;
|
36
|
+
|
37
|
+
Vector3 dir=Camera.main.tranform.rotation*delta;
|
38
|
+
target.transform.rotation=Quarternion.LookAt(dir);
|
39
|
+
lastPos=p;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
```
|
44
|
+
線形補完付き
|
45
|
+
```c#
|
46
|
+
public class SwipeLookAt:MonoBehavior{
|
47
|
+
public GameObject target;
|
48
|
+
Vector3 lastPos;
|
49
|
+
Quarternion q;
|
50
|
+
void Update(){
|
51
|
+
p=Input.mousePosition;
|
52
|
+
if(Input.GetMouseButtonDown(0)){
|
53
|
+
lastPos=p;
|
54
|
+
}
|
55
|
+
else if(Input.GetMouseButton(0)){
|
56
|
+
Vector3 delta=p-lastPos;
|
57
|
+
|
58
|
+
Vector3 dir=Camera.main.tranform.rotation*delta;
|
59
|
+
q=Quarternion.LookAt(dir);
|
60
|
+
lastPos=p;
|
61
|
+
}
|
62
|
+
target.transform.rotation=Quarternion.Lerp(target.transform.rotation,q,Time.deltaTime);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
```
|
24
66
|
|
25
67
|
|
68
|
+
|
69
|
+
おまけ
|
26
70
|
3D空間上で納得できる移動にする方法を幾つか挙げておきます。
|
27
71
|
|
28
72
|
#### 座標直代入
|
2
回転のプログラムを追記
answer
CHANGED
@@ -1,5 +1,28 @@
|
|
1
1
|
質問頂きありがとうございます。
|
2
2
|
|
3
|
+
追記
|
4
|
+
回転のプログラム
|
5
|
+
```c#
|
6
|
+
public class SwipeRotate:MonoBehavior{
|
7
|
+
public GameObject target;
|
8
|
+
Vector3 lastPos;
|
9
|
+
void Update(){
|
10
|
+
p=Input.mousePosition;
|
11
|
+
if(Input.GetMouseButtonDown(0)){
|
12
|
+
lastPos=p;
|
13
|
+
}
|
14
|
+
else if(Input.GetMouseButton(0)){
|
15
|
+
Vector3 delta=p-lastPos;
|
16
|
+
Vector3 normal=Quarternion.Euler(0,0,90)*delta;
|
17
|
+
Vector3 axis=Camera.main.tranform.rotation*normal;
|
18
|
+
Camera.main.transform.rotation*=Quarternion.AngleAxis(delta.magnitude,axis.normalized);
|
19
|
+
lastPos=p;
|
20
|
+
}
|
21
|
+
}
|
22
|
+
}
|
23
|
+
```
|
24
|
+
|
25
|
+
|
3
26
|
3D空間上で納得できる移動にする方法を幾つか挙げておきます。
|
4
27
|
|
5
28
|
#### 座標直代入
|
1
取りあえずエラーを直しました
answer
CHANGED
@@ -36,7 +36,7 @@
|
|
36
36
|
public GameObject target;
|
37
37
|
Vector3 lastPos;
|
38
38
|
void Update(){
|
39
|
-
Vector3 p=Camera.main.ScreenToWorldPoint(Input.mousePosition.x,Input.mousePosition.y,Vector3.Distance(target.transform.position,Camera.main.transform.position));
|
39
|
+
Vector3 p=Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,Vector3.Distance(target.transform.position,Camera.main.transform.position))));
|
40
40
|
if(Input.GetMouseButtonDown(0)){
|
41
41
|
lastPos=p;
|
42
42
|
}
|