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?

【Unity】オブジェクトの中心座標の取り方

Posted at

これできるの初めて知った
Markdown便利ね~

というのは置いといて中心座標の取り方です。

  // ブロックの中心座標 
  Vector3 BlockCenter()
  {
      // カメラからマウスへrayをとばす
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      RaycastHit hit;
      Renderer renderer;
      // 未割当でエラーになるためzero入れている
      Vector3 center = Vector3.zero;
      
      if (Physics.Raycast(ray, out hit))
      {
          renderer = hit.transform.GetComponent<Renderer>();
          // オブジェクトの範囲の中心
          if (renderer != null) center = renderer.bounds.center;
      }
      return center;
  }
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?