質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

761閲覧

unityちゃんを動かす向きを変えたい

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2019/10/08 10:06

編集2019/10/08 10:08

Standard Assetsを使って人型3Dモデルを動かす向きを変えたいのですがうまくいきません。
Standard AssetsのThirdPersonUserControl ではカメラの向きを取得してその方向へ3Dモデルを動かすスクリプトが組まれています。カメラの向きを変えながらまっすぐ動かしたいためPoleというオブジェクトを作り、cameraの代わりに向きを取得させたいのですが__ The name 'Pole' does not exist in the current context__というエラーが出てきてしまい取得出来ません。しかしtestというオブジェクトを作成してpoleの情報を取得させるとエラーを吐かず取得出来ます。どのようにしたらPoleの向きへ動かすことが出来ますか?

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Pole : MonoBehaviour 6{ 7 public static Pole m_instance; 8 //public static Pole pole_instance; 9 // Start is called before the first frame update 10 private Vector3 poleplayer_position; 11 private Vector3 poleplayer_forward; 12 private Vector3 poleplayer_rotate; 13 void Start() 14 { 15 m_instance=this; 16 //pole_instance=this; 17 18 } 19 20 // Update is called once per frame 21 void Update() 22 { 23 poleplayer_position=ThirdPersonUserControl.m_instance.transform.position;//自機のポジション 24 poleplayer_position=test.m_instance.transform.position; 25 //Debug.Log(poleplayer_position); 26 } 27} 28

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class test : MonoBehaviour 6{ 7 // Start is called before the first frame update 8 public static test m_instance; 9 private Vector3 poleplayer_position; 10 void Start() 11 { 12 m_instance=this; 13 } 14 15 // Update is called once per frame 16 void Update() 17 { 18 poleplayer_position=Pole.m_instance.transform.position; 19 Debug.Log(poleplayer_position); 20 } 21} 22

Update()直下にpoleの情報を取得するスクリプトを書いています

C#

1using System; 2using UnityEngine; 3using UnityStandardAssets.CrossPlatformInput; 4using System.Collections; 5using System.Collections.Generic; 6 7namespace UnityStandardAssets.Characters.ThirdPerson 8{ 9[RequireComponent(typeof (ThirdPersonCharacter))] 10public class ThirdPersonUserControl : MonoBehaviour 11{ 12 private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object 13 private Transform m_Cam; // A reference to the main camera in the scenes transform 14 private Vector3 m_CamForward; // The current forward direction of the camera 15 private Vector3 m_Move; 16 private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input. 17 public static ThirdPersonUserControl m_instance; 18 19 //private Transform pole_trans; 20 private Vector3 pole_position; 21 22 private void Start() 23 { 24 m_instance=this; 25 //pole_trans=Pole.pole_instance.transform; 26 //pole_position=Pole.m_instance.transform.position; 27 //if(Pole.m_instance==null) {Debug.Log("Pole null");} 28 29 // get the transform of the main camera 30 if (Camera.main != null) 31 { 32 m_Cam = Camera.main.transform; 33 } 34 else 35 { 36 Debug.LogWarning( 37 "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject); 38 // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them! 39 } 40 // get the third person character ( this should never be null due to require component ) 41 m_Character = GetComponent<ThirdPersonCharacter>(); 42 } 43 44 45 private void Update() 46 { 47 pole_position=Pole.m_instance.transform; 48 if (!m_Jump) 49 { 50 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump"); 51 } 52 } 53 54 55 // Fixed update is called in sync with physics 56 private void FixedUpdate() 57 { 58 // read inputs 59 float h = CrossPlatformInputManager.GetAxis("Horizontal");//十字キー取得 60 //float v = CrossPlatformInputManager.GetAxis("Vertical"); 61 float v=1f; 62 bool crouch = Input.GetKey(KeyCode.C); 63 // calculate move direction to pass to character 64 if (m_Cam != null) 65 { 66 // calculate camera relative direction to move: 67 m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized; 68 m_Move = v*m_CamForward + h*m_Cam.right; 69 } 70 else 71 { 72 // we use world-relative directions in the case of no main camera 73 m_Move = v*Vector3.forward + h*Vector3.right; 74 } 75#if !MOBILE_INPUT 76 // walk speed multiplier 77 if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f; 78#endif 79 80 // pass all parameters to the character control script 81 //Debug.Log(h+":"+v); 82 m_Character.Move(m_Move, crouch, m_Jump); 83 m_Jump = false; 84 } 85} 86}

スクリプトを置く場所が原因かと思い、SSも上げておきます
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

問題点・適切でない点

Update()直下の文

C#

1pole_position=Pole.m_instance.transform;

に"Pole"とありますが、"Pole"はスクリプト内で宣言されていません。
原則クラス参照は同じコード内で記述しますので↓

解決策

ThirdPersonUserControlクラス内に

C#

1public GameObject Pole;

を記述して、エディターから参照を渡してあげるとできると思います。

投稿2019/10/09 13:03

編集2019/10/11 13:48
KanazawaKureha

総合スコア368

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/10/09 23:44

回答ありがとうございます。Poleの向きを使って動かすことが出来ました。 しかしなぜtestとPoleは同じように宣言して参照せずとも情報を取得出来たのでしょうか?
KanazawaKureha

2019/10/11 13:58

スクリプトThirdPersonUserControl内に "namespace UnityStandardAssets.Characters.ThirdPerson{" という文章が記述されています。 つまりThirdPersonUserControlクラスはUnityStandardAssets.Characters.ThirdPerson名前空間に記述されており「全く別の位置」に書かれていたのでPoleクラスを見つけることができなかったわけです。 詳しいことはこちらを参照>>https://dkrevel.com/makegame-beginner/namespace/ 蛇足ですがこの"namespace...ThirdPerson{"という文を消すと正常に動作するように思えるかもしれませんが多分エラーを出されるのでやめた方がいいです。
退会済みユーザー

退会済みユーザー

2019/10/16 00:43

ありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問