「Unity/Csharp/位置と回転」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→指定場所へ徐々に移動) |
(→指定場所へ徐々に移動) |
||
| 行53: | 行53: | ||
==指定場所へ徐々に移動== | ==指定場所へ徐々に移動== | ||
| − | + | 毎フレーム処理するとよい。 | |
| + | 返されるpositionは、少し移動した座標となる。 | ||
<pre> | <pre> | ||
| − | Vector3 startPosition = | + | // Update is called once per frame |
| − | Vector3 endPosition = new Vector3( | + | void Update() |
| − | float speed = | + | { |
| − | float step = speed * Time.deltaTime; | + | Vector3 startPosition = transform.localPosition; |
| − | + | Vector3 endPosition = new Vector3(100f, 0f, 0f); | |
| − | + | float speed = 50f; | |
| − | + | float step = speed * Time.deltaTime; | |
| − | + | this.transform.localPosition = Vector3.MoveTowards( | |
| − | + | startPosition, | |
| + | endPosition, | ||
| + | step | ||
| + | ); | ||
| + | } | ||
</pre> | </pre> | ||
参考:https://docs.unity3d.com/ja/560/ScriptReference/Vector3.MoveTowards.html | 参考:https://docs.unity3d.com/ja/560/ScriptReference/Vector3.MoveTowards.html | ||
2024年7月13日 (土) 05:03時点における版
中間位置
Vector3 center = Vector3.Lerp(fromObj, toObj, 0.5f);
3dの回転
Unity/3d/回転 [ショートカット]
方向
以下それぞれ2つは同じ意味
new Vector3(0f, 0f, 0f) Vector3.zero
new Vector3(1f, 1f, 1f) Vector3.one
new Vector3(2f, 2f, 2f) Vector3.one * 2;
new Vector3(0, 1, 0) Vector3.up
Vector3(0, -1, 0) Vector3.down
Vector3(1, 0, 0) Vector3.right
Vector3(-1, 0, 0) Vector3.left
Vector3(0, 0, 1) Vector3.forward
Vector3(0, 0, -1) Vector3.back
指定場所へ徐々に移動
毎フレーム処理するとよい。 返されるpositionは、少し移動した座標となる。
// Update is called once per frame
void Update()
{
Vector3 startPosition = transform.localPosition;
Vector3 endPosition = new Vector3(100f, 0f, 0f);
float speed = 50f;
float step = speed * Time.deltaTime;
this.transform.localPosition = Vector3.MoveTowards(
startPosition,
endPosition,
step
);
}
参考:https://docs.unity3d.com/ja/560/ScriptReference/Vector3.MoveTowards.html
