46
40

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]オブジェクトの座標の取得と変更

Posted at

座標取得しようとしたら、うまくいかなかったのでその方法をメモ

#環境
・Unity 5.4.0f3

#コード

間違ってるコード
GameObject.Find("hogehoge").transform.position.x += 100;

こんな感じで、シーンからhogehogeオブジェクトを探して、そのx座標に100を加算したかった。
でも、これだとエラーが出たのでNG

正解のコード
Vector3 tmp = GameObject.Find("hogehoge").transform.position;
GameObject.Find("hogehoge").transform.position = new Vector3(tmp.x + 100, tmp.y, tmp.z);

うーん面倒臭い。
一度座標を取り出してから、再度代入という形にしないといけないみたい。

個別の座標を取るには、

float x = tmp.x;
float y = tmp.y;
float z = tmp.z;

でできる。

46
40
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
46
40

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?