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

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

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

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Unity3D

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

Unity

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

Q&A

1回答

1418閲覧

数秒間走った際に足の速度を変更する Unity3D

hikaaaaaaaa

総合スコア19

C#

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Unity3D

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

Unity

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

0グッド

1クリップ

投稿2020/04/20 05:32

Unity 3DでのゲームをStanderd Assetsを使って作成しております。

プレイヤーが数秒間走った際にライフを減らしていき、ゼロになった際に足の速さを0にするスクリプトを組もうとしているのですが、下記のスクリプトですと、プレイヤーを動かした際に5秒後に足の速さは0になるのですが、少しだけプレイヤーを動かした際にも5秒後にスピードが0になってしまいます。(time計測が走った際に開始されてしまうからだと思うのですが。。)

今後、ここにライフを3つ作成して3秒走るごとにライフが1つ減り、9秒後にはライフがゼロ。走れなくなった際に待つとライフが自然に2秒ごとに1回復。といったスクリプトを組みたいと考えているのですが、その前の上記の段階で止まってしまっております。

どなたかアドバイスいただけますと幸いです。

ThirdPersonCharacterではUpdateAnimatorメソッドを使用した際にismoveをtrueにするスクリプトを加えました。

C#

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

プレイヤーのコントローラー

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class PlayerController : MonoBehaviour 7{ 8 9 [SerializeField] private UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter anotherScript; 10 11 float span = 5.0f; 12 float delta = 0; 13 14 void Start() 15 { 16 17 18 anotherScript = GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter>(); 19 20 21 22 } 23 24 void Update() 25 { 26 if (anotherScript.isMove) 27 { 28 this.delta += Time.deltaTime; 29 if (this.delta > this.span) 30 { 31 SetValue(0); 32 Debug.Log("スピード0"); 33 34 } 35 } 36 37 38 39 } 40 41 42 void SetValue(int value) 43 { 44 anotherScript.M_MoveSpeedMultiplier = value; 45 46 //Debug.Log("M_MoveSpeedMultiplie = " + anotherScript.M_MoveSpeedMultiplier); 47 } 48 49}

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

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

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

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

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

guest

回答1

0

走っているときにスタミナを減らしていくスクリプト

C#

1 bool runnnig; //走っているときにtrue 2 float speed; //記録用のスピード 3 float playerSpeed = 0; //実際にプレイヤーが動くスピード 4 //speedとplayerSpeedは最初は同じ数値だが、speedはスタミナ回復し終わった時の代入に使う 5 6 float count = 0; //数える用 7 8 void Update() 9 { 10 if(running) 11 { 12 count += Time.deltatime; 13 if(count >= 5) 14 { 15 //5秒走った 16 count = 0; 17 running = false; 18 playerSpeed = 0; 19 20 //5秒後にスピードを普通に戻す 21 Invoke("SpeedModosu", 5); 22 } 23 } 24 else 25 { 26 //走っていなければ0にする 27 count = 0; 28 } 29 } 30 31 void SpeedModosu() 32 { 33 playerSpeed = speed; 34 }

投稿2020/04/20 09:35

Yukirr4_

総合スコア728

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問