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}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/10/09 23:44
2019/10/11 13:58
退会済みユーザー
2019/10/16 00:43