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

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

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

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

Q&A

解決済

2回答

3770閲覧

unity 動く床とプレイヤーの処理について

yoshiteru21

総合スコア44

Unity

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

1グッド

0クリップ

投稿2019/08/22 05:22

編集2019/08/26 01:56

動く床とプレイヤーが一緒に動くスクリプトです。
しかし、プレイヤーだけ動かず着地地点に固定されたままです。
原因をご教授ください。
タグはしっかり設定しています。
デバッグログで接触していることも検知してあります。
プレイヤーを動かしているスクリプトを貼っています。
もう一つスクリプトがありますが、関係ないとみて貼っていません。

namespace

1{ 2 [RequireComponent(typeof(Rigidbody))] 3 [RequireComponent(typeof(CapsuleCollider))] 4 [RequireComponent(typeof(Animator))] 5 public class ThirdPersonCharacter : MonoBehaviour 6 { 7 [SerializeField] float m_MovingTurnSpeed = 360; 8 [SerializeField] float m_StationaryTurnSpeed = 180; 9 [SerializeField] float m_JumpPower = 12f; 10 [Range(1f, 4f)][SerializeField] float m_GravityMultiplier = 2f; 11 [SerializeField] float m_RunCycleLegOffset = 0.2f; //specific to the character in sample assets, will need to be modified to work with others 12 [SerializeField] float m_MoveSpeedMultiplier = 1f; 13 [SerializeField] float m_AnimSpeedMultiplier = 1f; 14 [SerializeField] float m_GroundCheckDistance = 0.1f; 15 16 Rigidbody m_Rigidbody; 17 Animator m_Animator; 18 bool m_IsGrounded; 19 float m_OrigGroundCheckDistance; 20 const float k_Half = 0.5f; 21 float m_TurnAmount; 22 float m_ForwardAmount; 23 Vector3 m_GroundNormal; 24 float m_CapsuleHeight; 25 Vector3 m_CapsuleCenter; 26 CapsuleCollider m_Capsule; 27 bool m_Crouching; 28 29 30 void Start() 31 { 32 m_Animator = GetComponent<Animator>(); 33 m_Rigidbody = GetComponent<Rigidbody>(); 34 m_Capsule = GetComponent<CapsuleCollider>(); 35 m_CapsuleHeight = m_Capsule.height; 36 m_CapsuleCenter = m_Capsule.center; 37 38 m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ; 39 m_OrigGroundCheckDistance = m_GroundCheckDistance; 40 } 41 42 43 public void Move(Vector3 move, bool crouch, bool jump) 44 { 45 46 // convert the world relative moveInput vector into a local-relative 47 // turn amount and forward amount required to head in the desired 48 // direction. 49 if (move.magnitude > 1f) move.Normalize(); 50 move = transform.InverseTransformDirection(move); 51 CheckGroundStatus(); 52 move = Vector3.ProjectOnPlane(move, m_GroundNormal); 53 m_TurnAmount = Mathf.Atan2(move.x, move.z); 54 m_ForwardAmount = move.z; 55 56 ApplyExtraTurnRotation(); 57 58 // control and velocity handling is different when grounded and airborne: 59 if (m_IsGrounded) 60 { 61 HandleGroundedMovement(crouch, jump); 62 } 63 else 64 { 65 HandleAirborneMovement(); 66 } 67 68 ScaleCapsuleForCrouching(crouch); 69 PreventStandingInLowHeadroom(); 70 71 // send input and other state parameters to the animator 72 UpdateAnimator(move); 73 } 74 75 76 void ScaleCapsuleForCrouching(bool crouch) 77 { 78 if (m_IsGrounded && crouch) 79 { 80 if (m_Crouching) return; 81 m_Capsule.height = m_Capsule.height / 2f; 82 m_Capsule.center = m_Capsule.center / 2f; 83 m_Crouching = true; 84 } 85 else 86 { 87 Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up); 88 float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half; 89 if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore)) 90 { 91 m_Crouching = true; 92 return; 93 } 94 m_Capsule.height = m_CapsuleHeight; 95 m_Capsule.center = m_CapsuleCenter; 96 m_Crouching = false; 97 } 98 } 99 100 void PreventStandingInLowHeadroom() 101 { 102 // prevent standing up in crouch-only zones 103 if (!m_Crouching) 104 { 105 Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up); 106 float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half; 107 if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore)) 108 { 109 m_Crouching = true; 110 } 111 } 112 } 113 114 115 void UpdateAnimator(Vector3 move) 116 { 117 // update the animator parameters 118 m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime); 119 m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime); 120 m_Animator.SetBool("Crouch", m_Crouching); 121 m_Animator.SetBool("OnGround", m_IsGrounded); 122 if (!m_IsGrounded) 123 { 124 m_Animator.SetFloat("Jump", m_Rigidbody.velocity.y); 125 } 126 127 // calculate which leg is behind, so as to leave that leg trailing in the jump animation 128 // (This code is reliant on the specific run cycle offset in our animations, 129 // and assumes one leg passes the other at the normalized clip times of 0.0 and 0.5) 130 float runCycle = 131 Mathf.Repeat( 132 m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime + m_RunCycleLegOffset, 1); 133 float jumpLeg = (runCycle < k_Half ? 1 : -1) * m_ForwardAmount; 134 if (m_IsGrounded) 135 { 136 m_Animator.SetFloat("JumpLeg", jumpLeg); 137 } 138 139 // the anim speed multiplier allows the overall speed of walking/running to be tweaked in the inspector, 140 // which affects the movement speed because of the root motion. 141 if (m_IsGrounded && move.magnitude > 0) 142 { 143 m_Animator.speed = m_AnimSpeedMultiplier; 144 } 145 else 146 { 147 // don't use that while airborne 148 m_Animator.speed = 1; 149 } 150 } 151 152 153 void HandleAirborneMovement() 154 { 155 // apply extra gravity from multiplier: 156 Vector3 extraGravityForce = (Physics.gravity * m_GravityMultiplier) - Physics.gravity; 157 m_Rigidbody.AddForce(extraGravityForce); 158 159 m_GroundCheckDistance = m_Rigidbody.velocity.y < 0 ? m_OrigGroundCheckDistance : 0.01f; 160 } 161 162 163 void HandleGroundedMovement(bool crouch, bool jump) 164 { 165 // check whether conditions are right to allow a jump: 166 if (jump && !crouch && m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded")) 167 { 168 // jump! 169 m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z); 170 m_IsGrounded = false; 171 m_Animator.applyRootMotion = false; 172 m_GroundCheckDistance = 0.1f; 173 } 174 } 175 176 void ApplyExtraTurnRotation() 177 { 178 // help the character turn faster (this is in addition to root rotation in the animation) 179 float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount); 180 transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0); 181 } 182 183 184 public void OnAnimatorMove() 185 { 186 // we implement this function to override the default root motion. 187 // this allows us to modify the positional speed before it's applied. 188 if (m_IsGrounded && Time.deltaTime > 0) 189 { 190 Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime; 191 192 // we preserve the existing y part of the current velocity. 193 v.y = m_Rigidbody.velocity.y; 194 m_Rigidbody.velocity = v; 195 } 196 } 197 198 199 void CheckGroundStatus() 200 { 201 RaycastHit hitInfo; 202#if UNITY_EDITOR 203 // helper to visualise the ground check ray in the scene view 204 Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance)); 205#endif 206 // 0.1f is a small offset to start the ray from inside the character 207 // it is also good to note that the transform position in the sample assets is at the base of the character 208 if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance)) 209 { 210 m_GroundNormal = hitInfo.normal; 211 m_IsGrounded = true; 212 m_Animator.applyRootMotion = true; 213 } 214 else 215 { 216 m_IsGrounded = false; 217 m_GroundNormal = Vector3.up; 218 m_Animator.applyRootMotion = false; 219 } 220 221 } 222 223 224 void OnCollisionEnter(Collision col) 225 { 226 if (col.gameObject.tag == "MoveStage") 227 { 228 transform.SetParent(col.transform); 229 } 230 } 231 232 void OnCollisionExit(Collision col) 233 { 234 if (col.gameObject.tag == "MoveStage") 235 { 236 transform.SetParent(null); 237 } 238 } 239 240 } 241} 242コード
bochan2👍を押しています

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2019/08/22 05:24

プレイヤーを歩かせたいのですか?
yoshiteru21

2019/08/22 05:57

動く床が右に動いたとすれば、プレイヤーも右に動くようにしたいのですが(自分で操作はしていません)、そうはなりません。
退会済みユーザー

退会済みユーザー

2019/08/22 05:58

空港にあるような横行きのエスカレーターみたいな感じにしたいのですか?
yoshiteru21

2019/08/22 06:24

いえ、右や左に動く床です。
退会済みユーザー

退会済みユーザー

2019/08/22 06:26

床が動いたらプレイヤーも一緒に同じ速度で同じ方向に動かしたいのですか?
yoshiteru21

2019/08/22 06:33

はい、その通りです。
退会済みユーザー

退会済みユーザー

2019/08/22 06:35

下のサイトはどうですか?
yoshiteru21

2019/08/22 06:46

上のスクリプトは下のサイトを用いたものとなっております。 参考済みです。
退会済みユーザー

退会済みユーザー

2019/08/22 06:47

そうなんですか!?それはそれは・・・すみませんm(_ _)m また色々考えてみます。サイトとかも探してみます。すみませんでした。
sakura_hana

2019/08/22 07:05

問題の切り分けを行ってください。(以下の確認結果を質問文に追記してください) 1.OnCollisionEnterの先頭(if文の外)に「Debug.Log(collision.gameObject.tag);」を追加して、床に乗った時に"MoveFloor"が表示されるか確認してください。(されなかったら床との衝突自体が起こっていません。Colliderやタグの見直しを行ってください) 2.「MoveFloorタグを付けたオブジェクト」自体が動いていることを確認してください。 (例えば「実際に動いているのはMoveFloorタグを付けたオブジェクトの子オブジェクト」とかだとプレイヤーは動きません) 3.プレイを一時停止し、手動で(ヒエラルキーを操作して)プレイヤーが動く床の子オブジェクトに&位置も動く床の上になるようにして、プレイを再開するとどうなるか確認してください。 4.プレイヤーの動きを固定するようなスクリプトが無いか確認してください。 (例えば止まっている最中は「transform.position = 前フレームで計算したプレイヤーの位置座標;」にしているとか)
yoshiteru21

2019/08/22 07:38

tonko様ありがとうございます。sakura様が記載されたことをやってみて自分で改善していこうと思います。 sakura様ありがとうございます。確認いたします。
退会済みユーザー

退会済みユーザー

2019/08/22 07:39

いえいえ、いいですよ。
guest

回答2

0

投稿2019/08/22 06:28

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

自己解決

床のスクリプト事態に問題がありました。

投稿2019/08/26 02:13

yoshiteru21

総合スコア44

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問