Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9301

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

$
0
0

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

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

voidUpdate(){GameObjectobject=GameObject.Find("他のGameObjectの名前");Vector3pos=object.transform.position;floatspeed=30.0f;floatstep=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
などとして位置を調整することも可能です。

以上になります。


Viewing all articles
Browse latest Browse all 9301

Trending Articles