LoginSignup
41

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;

でできる。

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
41