前提
Unityで3Dのランゲームを制作しています。
Cinemachine Dolly Track Cart と Cinemachine Smooth Pathを使い、
以下リンクで解説されているような方法でコース上にPathを作成することで、キャラクターが自動的に進行するようにしていますが、フリック操作でキャラを任意で左右にも動かせる仕様を実装しようとしています。
Unityレースゲームの作り方7 Chinemachineの使い方 NPCの対戦相手を作成
(https://3dunity.org/game-create-lesson/racing-game/cpu-making-chinemachine/)
発生している問題
キャラクター自体に、後述するスクリプトをアタッチして左右移動できるようにしようと試みたのですが、
パス上を移動している間はCinemachineにポジションをコントロールされているため、このスクリプトが効きません。(Dolly Cartを使わずに,左右移動のみであればスクリプトが通りに動いている状態です。)
Dolly Cartのスクリプトをカスタマイズして、Cinemachineのパス上でキャラ自体のx座標を操作できるようにしたいのですが、どのようにプログラムを記述するべきか検討が付かず、質問を投稿させていただきました。
該当のソースコード
C#
1//以下コードはキャラを動かすスクリプトの一部を抜き出しています。この動作をCinemachineのPath上でも実現できるようにしたいです。 2 3 Vector3 startTouchPos; 4 Vector3 endTouchPos; 5 float flickValue_x; 6 7void Update() 8 { 9 //Flick Control 10 if (Input.GetMouseButtonDown(0) == true) 11 { 12 startTouchPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z); 13 } 14 if (Input.GetMouseButtonUp(0) == true) 15 { 16 endTouchPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z); 17 FlickDirection(); 18 GetDirection(); 19 } 20 } 21 void FlickDirection() 22 { 23 flickValue_x = endTouchPos.x - startTouchPos.x; 24 } 25 26 void GetDirection() 27 { 28 if (flickValue_x > 300.0f) 29 { 30 transform.position -= new Vector3(-4f, 0, 0); 31 } 32 33 if (flickValue_x < -300.0f) 34 { 35 transform.position += new Vector3(-4f, 0, 0); 36 } 37 }
試したこと
CinemachineDollyCart.csのスクリプトの以下記述でポジションを管理していることはわかったが、どのように考えるべきかわからない。
C#
1 /// <summary>The cart's current position on the path, in distance units</summary> 2 [Tooltip("The position along the path at which the cart will be placed. This can be animated directly or, if the velocity is non-zero, will be updated automatically. The value is interpreted according to the Position Units setting.")] 3 [FormerlySerializedAs("m_CurrentDistance")] 4 public float m_Position;
補足情報(FW/ツールのバージョンなど)
Unity Version 2020.3.30f1 Personal
Cinemachine 2.6.11
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー