Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
30
Help us understand the problem. What is going on with this article?
@haifuri

[Unity]オブジェクトの座標の取得と変更

More than 3 years have passed since last update.

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

環境

・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;

でできる。

30
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
haifuri
情報系の院生です

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
30
Help us understand the problem. What is going on with this article?