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

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

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

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

Unity

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

Q&A

解決済

1回答

1886閲覧

OVRPlayerControllerの子要素にしているObjectからOVRPlayerControllerの移動速度の値をスクリプトで取得する方法を教えてください

PotePui

総合スコア69

C#

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

Unity

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

0グッド

0クリップ

投稿2020/08/18 00:36

前提・実現したいこと

OVRPlayerControllerの子要素に、
オブジェクトを配置しています。
そして、そのOBJからOVRPlayerControllerの現在の移動速度を取得してくる方法が知りたい。

なぜ取得したいのかは、
取得して、その速度に応じてアニメーションを実装したいから、です。

発生している問題・エラーメッセージ

上記、実現したいことができず、
OVRPlayerContollerは基本的に一人称視点での操作ですが、
3人称にした場合に、キャラクタの動作アニメーションも移動速度やジャンプなど、AnimationControllerで付けたいです。

該当のソースコード

以下のスクリプトで
ここから、現在の移動速度を取得したいです
OVRPlayerContoller.cs

C#

1using System; 2using UnityEngine; 3 4[RequireComponent(typeof(CharacterController))] 5public class OVRPlayerController : MonoBehaviour 6{ 7 /// <summary> 8 /// The rate acceleration during movement. 9 /// </summary> 10 public float Acceleration = 0.1f; 11 12 /// <summary> 13 /// The rate of damping on movement. 14 /// </summary> 15 public float Damping = 0.3f; 16 17 /// <summary> 18 /// The rate of additional damping when moving sideways or backwards. 19 /// </summary> 20 public float BackAndSideDampen = 0.5f; 21 22 /// <summary> 23 /// The force applied to the character when jumping. 24 /// </summary> 25 public float JumpForce = 0.3f; 26 27 /// <summary> 28 /// The rate of rotation when using a gamepad. 29 /// </summary> 30 public float RotationAmount = 1.5f; 31 32 /// <summary> 33 /// The rate of rotation when using the keyboard. 34 /// </summary> 35 public float RotationRatchet = 45.0f; 36 37 /// <summary> 38 /// The player will rotate in fixed steps if Snap Rotation is enabled. 39 /// </summary> 40 [Tooltip("The player will rotate in fixed steps if Snap Rotation is enabled.")] 41 public bool SnapRotation = true; 42 43 /// <summary> 44 /// How many fixed speeds to use with linear movement? 0=linear control 45 /// </summary> 46 [Tooltip("How many fixed speeds to use with linear movement? 0=linear control")] 47 public int FixedSpeedSteps; 48 49 /// <summary> 50 /// If true, reset the initial yaw of the player controller when the Hmd pose is recentered. 51 /// </summary> 52 public bool HmdResetsY = true; 53 54 /// <summary> 55 /// If true, tracking data from a child OVRCameraRig will update the direction of movement. 56 /// </summary> 57 public bool HmdRotatesY = true; 58 59 /// <summary> 60 /// Modifies the strength of gravity. 61 /// </summary> 62 public float GravityModifier = 0.379f; 63 64 /// <summary> 65 /// If true, each OVRPlayerController will use the player's physical height. 66 /// </summary> 67 public bool useProfileData = true; 68 69 /// <summary> 70 /// The CameraHeight is the actual height of the HMD and can be used to adjust the height of the character controller, which will affect the 71 /// ability of the character to move into areas with a low ceiling. 72 /// </summary> 73 [NonSerialized] 74 public float CameraHeight; 75 76 /// <summary> 77 /// This event is raised after the character controller is moved. This is used by the OVRAvatarLocomotion script to keep the avatar transform synchronized 78 /// with the OVRPlayerController. 79 /// </summary> 80 public event Action<Transform> TransformUpdated; 81 82 /// <summary> 83 /// This bool is set to true whenever the player controller has been teleported. It is reset after every frame. Some systems, such as 84 /// CharacterCameraConstraint, test this boolean in order to disable logic that moves the character controller immediately 85 /// following the teleport. 86 /// </summary> 87 [NonSerialized] // This doesn't need to be visible in the inspector. 88 public bool Teleported; 89 90 /// <summary> 91 /// This event is raised immediately after the camera transform has been updated, but before movement is updated. 92 /// </summary> 93 public event Action CameraUpdated; 94 95 /// <summary> 96 /// This event is raised right before the character controller is actually moved in order to provide other systems the opportunity to 97 /// move the character controller in response to things other than user input, such as movement of the HMD. See CharacterCameraConstraint.cs 98 /// for an example of this. 99 /// </summary> 100 public event Action PreCharacterMove; 101 102 /// <summary> 103 /// When true, user input will be applied to linear movement. Set this to false whenever the player controller needs to ignore input for 104 /// linear movement. 105 /// </summary> 106 public bool EnableLinearMovement = true; 107 108 /// <summary> 109 /// When true, user input will be applied to rotation. Set this to false whenever the player controller needs to ignore input for rotation. 110 /// </summary> 111 public bool EnableRotation = true; 112 113 /// <summary> 114 /// Rotation defaults to secondary thumbstick. You can allow either here. Note that this won't behave well if EnableLinearMovement is true. 115 /// </summary> 116 public bool RotationEitherThumbstick = false; 117 118 protected CharacterController Controller = null; 119 protected OVRCameraRig CameraRig = null; 120 121 private float MoveScale = 1.0f; 122 private Vector3 MoveThrottle = Vector3.zero; 123 private float FallSpeed = 0.0f; 124 private OVRPose? InitialPose; 125 public float InitialYRotation { get; private set; } 126 private float MoveScaleMultiplier = 1.0f; 127 private float RotationScaleMultiplier = 1.0f; 128 private bool SkipMouseRotation = true; // It is rare to want to use mouse movement in VR, so ignore the mouse by default. 129 private bool HaltUpdateMovement = false; 130 private bool prevHatLeft = false; 131 private bool prevHatRight = false; 132 private float SimulationRate = 60f; 133 private float buttonRotation = 0f; 134 private bool ReadyToSnapTurn; // Set to true when a snap turn has occurred, code requires one frame of centered thumbstick to enable another snap turn. 135 private bool playerControllerEnabled = false; 136 137 void Start() 138 { 139 // Add eye-depth as a camera offset from the player controller 140 var p = CameraRig.transform.localPosition; 141 p.z = OVRManager.profile.eyeDepth; 142 CameraRig.transform.localPosition = p; 143 } 144 145 public virtual void UpdateMovement() 146 { 147 148#if !UNITY_ANDROID // LeftTrigger not avail on Android game pad 149 moveInfluence *= 1.0f + OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger); 150#endif 151 152 Vector2 primaryAxis = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick); 153 154 // If speed quantization is enabled, adjust the input to the number of fixed speed steps. 155 if (FixedSpeedSteps > 0) 156 { 157 primaryAxis.y = Mathf.Round(primaryAxis.y * FixedSpeedSteps) / FixedSpeedSteps; 158 primaryAxis.x = Mathf.Round(primaryAxis.x * FixedSpeedSteps) / FixedSpeedSteps; 159 } 160 161 if (primaryAxis.y > 0.0f) 162 MoveThrottle += ort * (primaryAxis.y * transform.lossyScale.z * moveInfluence * Vector3.forward); 163 164 if (primaryAxis.y < 0.0f) 165 MoveThrottle += ort * (Mathf.Abs(primaryAxis.y) * transform.lossyScale.z * moveInfluence * 166 BackAndSideDampen * Vector3.back); 167 168 if (primaryAxis.x < 0.0f) 169 MoveThrottle += ort * (Mathf.Abs(primaryAxis.x) * transform.lossyScale.x * moveInfluence * 170 BackAndSideDampen * Vector3.left); 171 172 if (primaryAxis.x > 0.0f) 173 MoveThrottle += ort * (primaryAxis.x * transform.lossyScale.x * moveInfluence * BackAndSideDampen * 174 Vector3.right); 175 }

試したこと

子要素のオブジェクト(階層でいうと、OVRCameraRigと同じ)から親要素を取得してくるのは、

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class NewBehaviourScript : MonoBehaviour 6{ 7 8 public OVRPlayerController m_MoveSpeed; 9 // Start is called before the first frame update 10 void Start() 11 { 12 13 m_MoveSpeed = GetComponentInParent<OVRPlayerController>(); 14 //上記の「試したこと」内のコードでの、Vector2 primaryAxisでLThumbStickの傾きでPlayerが動くので、 15     //primaryAxis.x,primaryAxis.yでPlayerが動いているか静止しているかの判別が可能なので、その値の取得方法が知りたいです。 16 //以下に、どうしたら、PrimaryAxis.x,PrimaryAxis.yが0か0より大きいのか、子要素から取得する方法を教えてください。 17   18 //m_MoveSpeed.primary.Axis.y; 19 20 } 21}

補足情報(FW/ツールのバージョンなど)

Unity 2019.4.3f1

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

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

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

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

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

guest

回答1

0

ベストアンサー

primaryAxisUpdateMovementのローカル変数として定義されています。
変数のスコープについて学習されるべきかと思います。

投稿2020/08/18 00:52

YAmaGNZ

総合スコア10294

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

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

PotePui

2020/08/18 04:30

なるほどです。言われないと全く気付かなかったです。 ローカルなのかパブリックなのか意識したほうがいいですね。。 だから外部から値が参照できなかったのですね。理由が分かりました。 外部からアクセスできるように、PrimaryAxis変数を定義してみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問