回答編集履歴
1
訂正(コードを示しているためコメントではなく回答を修正した)
answer
CHANGED
@@ -1,3 +1,40 @@
|
|
1
|
+
RotateAround は obsolete だそうだけど、代わりに使うように指定されている Transform.Rotate で簡単に RotateAround と同じことができそうにないためそのまま RotateAround でやってみました。
|
2
|
+
|
3
|
+
```csharp
|
4
|
+
using UnityEngine;
|
5
|
+
|
6
|
+
public class Rotation : MonoBehaviour
|
7
|
+
{
|
8
|
+
[SerializeField] Transform m_pivot;
|
9
|
+
[SerializeField] float m_rotateAngle = 1f;
|
10
|
+
[SerializeField] float m_targetAngle = 90f;
|
11
|
+
float m_currentAngle = 0;
|
12
|
+
int m_spinDirection = 1;
|
13
|
+
|
14
|
+
void Update()
|
15
|
+
{
|
16
|
+
Turn();
|
17
|
+
}
|
18
|
+
|
19
|
+
void Turn()
|
20
|
+
{
|
21
|
+
if (m_spinDirection * m_targetAngle > m_spinDirection * m_currentAngle)
|
22
|
+
{
|
23
|
+
m_currentAngle += m_spinDirection * m_rotateAngle;
|
24
|
+
transform.RotateAround(m_pivot.position, Vector3.back, m_spinDirection * m_rotateAngle);
|
25
|
+
}
|
26
|
+
else
|
27
|
+
{
|
28
|
+
m_spinDirection *= -1;
|
29
|
+
m_targetAngle *= -1;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
```
|
35
|
+
|
36
|
+
(※)以下のコードは最初に示したものですが、回転の中心が自分自身であるため、質問の主旨(中心は別オブジェクト)とは違うものでした。
|
37
|
+
|
1
38
|
Unity本来の機能であるアニメーションで簡単に済ませたいけど、説明が大変なのでその次に楽そうな[DoTween](https://assetstore.unity.com/packages/tools/animation/dotween-hotween-v2-27676)を使ったやり方のコードを貼っておきます。
|
2
39
|
|
3
40
|
```csharp
|