facebook twitter hatena line email

「Unity/3d/ゆっくり移動」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(Sleapを使って球状にゆっくり移動を)
行18: 行18:
 
[SerializeField] GameObject endObj;
 
[SerializeField] GameObject endObj;
 
[SerializeField] GameObject nowObj;
 
[SerializeField] GameObject nowObj;
 +
float speed = 0.3f;
 
void Update()
 
void Update()
 
{
 
{
 
     float distance = Vector3.Distance(startObj.transform.localPosition, endObj.transform.localPosition);
 
     float distance = Vector3.Distance(startObj.transform.localPosition, endObj.transform.localPosition);
     float present = (Time.time * speed) / distance/ 10;
+
     float present = (Time.time * speed) / distance;
 
     nowObj.transform.localPosition = Vector3.Slerp(startObj.transform.localPosition, endObj.transform.localPosition, present);
 
     nowObj.transform.localPosition = Vector3.Slerp(startObj.transform.localPosition, endObj.transform.localPosition, present);
 
}</pre>
 
}</pre>

2022年12月10日 (土) 18:26時点における版

Leapを使って直線でゆっくり移動を

presentには0〜1を入れる。

float present = 0f; // 0~1
[SerializeField] GameObject startObj;
[SerializeField] GameObject endObj;
[SerializeField] GameObject nowObj;
void Update()
{
    present += 0.01f;
    nowObj.transform.localPosition = Vector3.Lerp(startObj.transform.localPosition, endObj.transform.localPosition, present);
}

Sleapを使って球状にゆっくり移動を

presentには0〜1を入れる。

[SerializeField] GameObject startObj;
[SerializeField] GameObject endObj;
[SerializeField] GameObject nowObj;
float speed = 0.3f;
void Update()
{
    float distance = Vector3.Distance(startObj.transform.localPosition, endObj.transform.localPosition);
    float present = (Time.time * speed) / distance;
    nowObj.transform.localPosition = Vector3.Slerp(startObj.transform.localPosition, endObj.transform.localPosition, present);
}