やりたいこと
Unityにて、マウスカーソルを動かし曲面状の壁(オブジェクト)にRayを当て、
壁の表面に沿ってアイテム(Cube)を生成・移動させるスクリプトを作りたいと思っています。
■参考画像(1)
問題点
今、躓いているのが「Quaternion型」を使った実装をすると動作しないことです。
■今実装中のコード
C#
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class BuildingManager : MonoBehaviour { public GameObject[] objects; private GameObject pendingObject; private Vector3 pos; private RaycastHit hit; [SerializeField] private LayerMask layerMask; // Update is called once per frame void Update() { if(pendingObject != null) { pendingObject.transform.position = pos; if (Input.GetMouseButtonDown(0)) { PlaceObject(); } } } public void PlaceObject() { pendingObject = null; } private void FixedUpdate() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 1000, layerMask)) { pos = hit.point; } } public void SelectObject(int index) { pendingObject = Instantiate(objects[index], pos, transform.rotation); } }
このスクリプトによるRayが当たった際の処理です。
C#
private void FixedUpdate() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 1000, layerMask)) { pos = hit.point; } }
pos = hit.point;の後の処理に
pos.rotation = Quaternion.LookRotation(Vector3.up, hit.normal); ←追加させたいコード
…といった「Quaternion」型を使って表面情報を反映させたいのですが、
「'Vector3' に 'rotation' の定義が含まれておらず、型 'Vector3' の最初の引数を受け付けるアクセス可能な拡張メソッド 'rotation' が見つかりませんでした。」
というエラーが出る為、pos 変数にそれらの情報を格納することができません。
どうやら「Quaternion」を使うためにはTransform型を宣言する必要があると存じているのですが…
恥ずかしながら、私にこれ以上の知識が無く困っています…。
どうぞご教授いただければと思います。よろしくお願いいたします。
まだ回答がついていません
会員登録して回答してみよう