やりたいこと
Unityにて、マウスカーソルを動かし曲面状の壁(オブジェクト)にRayを当て、
壁の表面に沿ってアイテム(Cube)を生成・移動させるスクリプトを作りたいと思っています。
■参考画像(1)
問題点
今、躓いているのが「Quaternion型」を使った実装をすると動作しないことです。
■今実装中のコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class BuildingManager : MonoBehaviour 7{ 8 public GameObject[] objects; 9 private GameObject pendingObject; 10 11 private Vector3 pos; 12 13 private RaycastHit hit; 14 15 [SerializeField] private LayerMask layerMask; 16 17 // Update is called once per frame 18 void Update() 19 { 20 if(pendingObject != null) 21 { 22 pendingObject.transform.position = pos; 23 24 if (Input.GetMouseButtonDown(0)) 25 { 26 PlaceObject(); 27 } 28 } 29 } 30 31 public void PlaceObject() 32 { 33 pendingObject = null; 34 } 35 36 private void FixedUpdate() 37 { 38 39 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 40 41 if (Physics.Raycast(ray, out hit, 1000, layerMask)) 42 { 43 pos = hit.point; 44 } 45 46 } 47 48 public void SelectObject(int index) 49 { 50 pendingObject = Instantiate(objects[index], pos, transform.rotation); 51 } 52}
このスクリプトによるRayが当たった際の処理です。
C#
1 private void FixedUpdate() 2 { 3 4 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 5 6 if (Physics.Raycast(ray, out hit, 1000, layerMask)) 7 { 8 pos = hit.point; 9 } 10 11 }
pos = hit.point;の後の処理に
pos.rotation = Quaternion.LookRotation(Vector3.up, hit.normal); ←追加させたいコード
…といった「Quaternion」型を使って表面情報を反映させたいのですが、
「'Vector3' に 'rotation' の定義が含まれておらず、型 'Vector3' の最初の引数を受け付けるアクセス可能な拡張メソッド 'rotation' が見つかりませんでした。」
というエラーが出る為、pos 変数にそれらの情報を格納することができません。
どうやら「Quaternion」を使うためにはTransform型を宣言する必要があると存じているのですが…
恥ずかしながら、私にこれ以上の知識が無く困っています…。
どうぞご教授いただければと思います。よろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/05/16 06:58