1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】transform.positionを変更して他のGameObjectの位置に移動する

Posted at

transform.positionを使用してGameObjectを移動させることはよくありますが、他のGameObjectの位置に移動させようとしたときに、躓いたので紹介していきたいと思います。

手順としては、他のGameObjectの位置を取得し、それをVector3.MoveTowardsを使用して移動させていきます。

void Update(){
  GameObject object = GameObject.Find("他のGameObjectの名前");
  Vector3 pos = object.transform.position;
  float speed = 30.0f;
  float step = speed * Time.deltaTime;
  transform.position = Vector3.MoveTowards(transform.position, pos, step);
}

GameObject.Find()でUnity上にある()内の名前のGameObjectを取得します。
そのGameObjectのtransform.positionをposに入れます。
そして、Vector3.MoveTowardsで現在位置であるtransform.positionからposへstepの速さで移動するようにします。

このとき、
pos.x = 1.0f
などとして位置を調整することも可能です。

以上になります。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?