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

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

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

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

Unity3D

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

Unity

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

Q&A

0回答

1493閲覧

Standered Assetsのプレイヤーの編集 Unity

hikaaaaaaaa

総合スコア19

C#

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

Unity3D

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

Unity

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

0グッド

0クリップ

投稿2020/04/20 15:32

編集2020/04/20 15:35

Standered Assetsを用いてUnityで3Dのゲームを作成しております。

ThirdPersonAnimatorControllerを使用しており、
その際に用いるThirdPersonUserControlとThirdPersonCharacterを利用してキャラクターを移動させております。

その際にもし、キャラクターが移動している際には処理を加えたいのでその際の判定をboolでつけようと考えているのですがどの部分にboolを書きtrueに返してあげればキャラクターの移動をしている最中のみに処理を加えられるのかわかりませんでした。(ThirdPersonCharacterのMoveメソッドのスコープ内にbool trueを加え、他のスクリプトで処理をしましたがプレイヤーが動いていない際にも反映がされてしまいました。)

もし、Standered Assetsを用いてキャラクターの動きの変更をした経験があったり、どの部分の修正を加えれば良いか分かる方はアドバイスいただけますと幸いです。

ThirdPersonCharacter

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 49 50 51 void Start() 52 { 53 m_Animator = GetComponent<Animator>(); 54 m_Rigidbody = GetComponent<Rigidbody>(); 55 m_Capsule = GetComponent<CapsuleCollider>(); 56 m_CapsuleHeight = m_Capsule.height; 57 m_CapsuleCenter = m_Capsule.center; 58 59 m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ; 60 m_OrigGroundCheckDistance = m_GroundCheckDistance; 61 } 62 63 64 public void Move(Vector3 move, bool crouch, bool jump) 65 { 66 67 if (move.magnitude > 1f) move.Normalize(); 68 move = transform.InverseTransformDirection(move); 69 CheckGroundStatus(); 70 move = Vector3.ProjectOnPlane(move, m_GroundNormal); 71 m_TurnAmount = Mathf.Atan2(move.x, move.z); 72 m_ForwardAmount = move.z; 73 74 ApplyExtraTurnRotation(); 75 76 77 if (m_IsGrounded) 78 { 79 HandleGroundedMovement(crouch, jump); 80 } 81 else 82 { 83 HandleAirborneMovement(); 84 } 85 86 ScaleCapsuleForCrouching(crouch); 87 PreventStandingInLowHeadroom(); 88 } 89 90 91 void ScaleCapsuleForCrouching(bool crouch) 92 { 93 if (m_IsGrounded && crouch) 94 { 95 if (m_Crouching) return; 96 m_Capsule.height = m_Capsule.height / 2f; 97 m_Capsule.center = m_Capsule.center / 2f; 98 m_Crouching = true; 99 } 100 else 101 { 102 Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up); 103 float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half; 104 if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore)) 105 { 106 m_Crouching = true; 107 return; 108 } 109 m_Capsule.height = m_CapsuleHeight; 110 m_Capsule.center = m_CapsuleCenter; 111 m_Crouching = false; 112 } 113 } 114 115 void PreventStandingInLowHeadroom() 116 { 117 if (!m_Crouching) 118 { 119 Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up); 120 float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half; 121 if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore)) 122 { 123 m_Crouching = true; 124 } 125 } 126 } 127 128 129 void UpdateAnimator(Vector3 move) 130 { 131 // update the animator parameters 132 m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime); 133 m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime); 134 m_Animator.SetBool("Crouch", m_Crouching); 135 m_Animator.SetBool("OnGround", m_IsGrounded); 136 if (!m_IsGrounded) 137 { 138 m_Animator.SetFloat("Jump", m_Rigidbody.velocity.y); 139 } 140 141 142 float runCycle = 143 Mathf.Repeat( 144 m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime + m_RunCycleLegOffset, 1); 145 float jumpLeg = (runCycle < k_Half ? 1 : -1) * m_ForwardAmount; 146 if (m_IsGrounded) 147 { 148 m_Animator.SetFloat("JumpLeg", jumpLeg); 149 } 150 151 // the anim speed multiplier allows the overall speed of walking/running to be tweaked in the inspector, 152 // which affects the movement speed because of the root motion. 153 if (m_IsGrounded && move.magnitude > 0) 154 { 155 m_Animator.speed = m_AnimSpeedMultiplier; 156 157 158 } 159 else 160 { 161 // don't use that while airborne 162 m_Animator.speed = 1; 163 } 164 } 165 166 167 void HandleAirborneMovement() 168 { 169 // apply extra gravity from multiplier: 170 Vector3 extraGravityForce = (Physics.gravity * m_GravityMultiplier) - Physics.gravity; 171 m_Rigidbody.AddForce(extraGravityForce); 172 173 m_GroundCheckDistance = m_Rigidbody.velocity.y < 0 ? m_OrigGroundCheckDistance : 0.01f; 174 } 175 176 177 void HandleGroundedMovement(bool crouch, bool jump) 178 { 179 // check whether conditions are right to allow a jump: 180 if (jump && !crouch && m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded")) 181 { 182 // jump! 183 m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z); 184 m_IsGrounded = false; 185 m_Animator.applyRootMotion = false; 186 m_GroundCheckDistance = 0.1f; 187 } 188 } 189 190 void ApplyExtraTurnRotation() 191 { 192 // help the character turn faster (this is in addition to root rotation in the animation) 193 float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount); 194 transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0); 195 } 196 197 198 public void OnAnimatorMove() 199 { 200 201 if (m_IsGrounded && Time.deltaTime > 0) 202 { 203 Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime; 204 205 206 v.y = m_Rigidbody.velocity.y; 207 m_Rigidbody.velocity = v; 208 209 210 211 } 212 } 213 214 215 void CheckGroundStatus() 216 { 217 RaycastHit hitInfo; 218#if UNITY_EDITOR 219 220 Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance)); 221#endif 222 223 if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance)) 224 { 225 m_GroundNormal = hitInfo.normal; 226 m_IsGrounded = true; 227 m_Animator.applyRootMotion = true; 228 } 229 else 230 { 231 m_IsGrounded = false; 232 m_GroundNormal = Vector3.up; 233 m_Animator.applyRootMotion = false; 234 } 235 } 236 } 237 238 239} 240 241 242

ThirdPersonUserControl

C#

1using System; 2using UnityEngine; 3using UnityStandardAssets.CrossPlatformInput; 4 5namespace UnityStandardAssets.Characters.ThirdPerson 6{ 7 [RequireComponent(typeof (ThirdPersonCharacter))] 8 public class ThirdPersonUserControl : MonoBehaviour 9 { 10 private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object 11 private Transform m_Cam; // A reference to the main camera in the scenes transform 12 private Vector3 m_CamForward; // The current forward direction of the camera 13 private Vector3 m_Move; 14 private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input. 15 16 17 private void Start() 18 { 19 // get the transform of the main camera 20 if (Camera.main != null) 21 { 22 m_Cam = Camera.main.transform; 23 } 24 else 25 { 26 Debug.LogWarning( 27 "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject); 28 29 } 30 31 32 m_Character = GetComponent<ThirdPersonCharacter>(); 33 } 34 35 36 private void Update() 37 { 38 if (!m_Jump) 39 { 40 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump"); 41 } 42 } 43 44 private void FixedUpdate() 45 { 46 47 float h = CrossPlatformInputManager.GetAxis("Horizontal"); 48 float v = CrossPlatformInputManager.GetAxis("Vertical"); 49 bool crouch = Input.GetKey(KeyCode.C); 50 51 52 if (m_Cam != null) 53 { 54 55 m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized; 56 m_Move = v*m_CamForward + h*m_Cam.right; 57 } 58 else 59 { 60 61 m_Move = v*Vector3.forward + h*Vector3.right; 62 } 63#if !MOBILE_INPUT 64 65 if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f; 66#endif 67 68 69 m_Character.Move(m_Move, crouch, m_Jump); 70 m_Jump = false; 71 } 72 } 73} 74

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問