回答編集履歴
2
コード中のコメントを追加
test
CHANGED
@@ -148,7 +148,7 @@
|
|
148
148
|
|
149
149
|
{
|
150
150
|
|
151
|
-
this.transform.rotation = Quaternion.LookRotation(motion);
|
151
|
+
this.transform.rotation = Quaternion.LookRotation(motion); // 入力に対する向き合わせのための回転はプレイヤー操作用原点のTransformを操作する
|
152
152
|
|
153
153
|
}
|
154
154
|
|
1
修正案を追記
test
CHANGED
@@ -3,3 +3,173 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
参考: [UnityでObjectのPivotを変更する - 脳汁portal](http://portaltan.hatenablog.com/entry/2016/04/15/134129)
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
###[追記]
|
10
|
+
|
11
|
+
下図のような、「プレイヤー操作用原点」→「Z軸周り回転用原点」→「ユニティちゃん」の三重構造にするのはどうでしょう。プレイヤー操作用原点は本来のユニティちゃんと同じく足下に位置するようにして、Z軸周り回転用原点をおヘソの位置に配置する感じです。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
![ヒエラルキー](52e3518084589d6ea03cd7c58d241822.gif)
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
本来のユニティちゃんの原点とプレイヤー操作用原点が一致するようにしておいて、CaracterControllerやプレイヤー制御用スクリプトはプレイヤー操作用原点にアタッチし、Z軸周り回転アクションだけはZ軸周り回転用原点を操作、それ以外はプレイヤー操作用原点を操作するようにすれば、今までの足下原点前提のコードを残したまま回転アクションを加えられるかと思います。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
実行時の様子
|
24
|
+
|
25
|
+
![アクション](5412e245592c07a5e75563dc15ac01ac.gif)
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
ちなみにご参考までに、上図のプレイヤー操作用原点のスクリプトは下記のようになっています。ご質問者さんの設計とは異なる部分が多々あるかと思いますので、あまりお役に立てないかもしれませんが...
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
```C#
|
34
|
+
|
35
|
+
using System.Collections;
|
36
|
+
|
37
|
+
using System.Linq;
|
38
|
+
|
39
|
+
using UnityEngine;
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
[RequireComponent(typeof(CharacterController))]
|
44
|
+
|
45
|
+
public class PlayerController : MonoBehaviour
|
46
|
+
|
47
|
+
{
|
48
|
+
|
49
|
+
public float MaxSpeed = 5.0f;
|
50
|
+
|
51
|
+
public Animator PlayerAnimator; // ユニティちゃんのAnimator
|
52
|
+
|
53
|
+
public Transform RotationPivot; // Z軸周り回転用原点
|
54
|
+
|
55
|
+
private Transform cameraTransform; // 移動制御のためのカメラのTransform(今回の話題とは直接関係ありません)
|
56
|
+
|
57
|
+
private CharacterController characterController; // プレイヤー操作用原点にアタッチされているCharacterController
|
58
|
+
|
59
|
+
private Coroutine rollingCoroutine;
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
// Z軸周り回転アクション
|
64
|
+
|
65
|
+
private IEnumerator Roll()
|
66
|
+
|
67
|
+
{
|
68
|
+
|
69
|
+
var angle = 0.0f;
|
70
|
+
|
71
|
+
while (angle < 360.0f)
|
72
|
+
|
73
|
+
{
|
74
|
+
|
75
|
+
this.RotationPivot.localEulerAngles = new Vector3(0.0f, 0.0f, angle); // Z軸周り回転はRotationPivotに関して行う
|
76
|
+
|
77
|
+
angle += 30.0f;
|
78
|
+
|
79
|
+
yield return null;
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
this.RotationPivot.localEulerAngles = Vector3.zero;
|
86
|
+
|
87
|
+
this.rollingCoroutine = null;
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
private void Start()
|
94
|
+
|
95
|
+
{
|
96
|
+
|
97
|
+
this.characterController = this.GetComponent<CharacterController>();
|
98
|
+
|
99
|
+
this.cameraTransform = Camera.main.transform;
|
100
|
+
|
101
|
+
if (this.PlayerAnimator == null)
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
this.PlayerAnimator = this.GetComponentInChildren<Animator>();
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
if (this.RotationPivot == null)
|
110
|
+
|
111
|
+
{
|
112
|
+
|
113
|
+
this.RotationPivot = this.GetComponentsInChildren<Transform>()
|
114
|
+
|
115
|
+
.Where(t => t.name == "RotationPivot")
|
116
|
+
|
117
|
+
.First();
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
private void Update()
|
126
|
+
|
127
|
+
{
|
128
|
+
|
129
|
+
// プレイヤーの移動
|
130
|
+
|
131
|
+
var input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
|
132
|
+
|
133
|
+
var inputMagnitude = Mathf.Clamp01(input.magnitude);
|
134
|
+
|
135
|
+
var right = this.cameraTransform.right;
|
136
|
+
|
137
|
+
var forward = this.cameraTransform.forward;
|
138
|
+
|
139
|
+
forward.y = 0.0f;
|
140
|
+
|
141
|
+
forward.Normalize();
|
142
|
+
|
143
|
+
var motion = ((input.x * right) + (input.y * forward)) * this.MaxSpeed;
|
144
|
+
|
145
|
+
var speed = motion.magnitude;
|
146
|
+
|
147
|
+
if (speed > 0.25f)
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
this.transform.rotation = Quaternion.LookRotation(motion);
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
this.characterController.SimpleMove(motion); // 移動はプレイヤー操作用原点のCharacterControllerを操作する
|
156
|
+
|
157
|
+
this.PlayerAnimator.SetFloat("Speed", inputMagnitude); // アニメーションはユニティちゃんのAnimatorを操作する
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
// スペースキー押下でZ軸周り回転アクション
|
162
|
+
|
163
|
+
if ((this.rollingCoroutine == null) && Input.GetKeyDown(KeyCode.Space))
|
164
|
+
|
165
|
+
{
|
166
|
+
|
167
|
+
this.rollingCoroutine = this.StartCoroutine(this.Roll());
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
```
|