前提・実現したいこと
Unityで特定の位置までオブジェクトを移動し、到達したら次の動きをするようにしたいです。
transformには「Vector3.MoveTowards」を使用しています。目的地に「Target」のオブジェクトを配置し、「Target.transform.position」で目的地の座標を取得しています。これで目的地まで動かすのはうまくいっています。
発生している問題・エラーメッセージ
問題はこの後で、「if (transform.position.y <= target0.transform.position.y)」という風に条件付けを行ったのですが、これがうまく作用するときとしない時があります。オブジェクトは、上から下に下がってくるような動きをしていて、ターゲットのY座標と同じになっているはずなのですがうまくいかない時があります。
エラーメッセージ
該当のソースコード
c#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shoukou1 : MonoBehaviour
{
public int ii = 0;
public float speed; float step;
// Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { step = speed * Time.deltaTime; if (jj == 0) { transform.position = Vector3.MoveTowards(transform.position, target0.transform.position, step); if (transform.position.y <= target0.transform.position.y) { ii =1} } }
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。