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

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

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

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

Unity3D

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

Q&A

解決済

1回答

1625閲覧

モーションの現在の再生時間と全体の長さが知りたい、攻撃コンボを実装させたい。

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

Unity3D

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

0グッド

0クリップ

投稿2020/03/28 10:19

void Update();部ですが攻撃モーションのコンボを作りたくて一定の間隔ないでZキーを押すと振り戻す前の攻撃コンボ2が再生されるという仕様を実装しようと思うのですが現在のモーション時間を知りたいです。調べると__float f += Time.deltatime;__を使う方法を参考サイトで調べましたがちょと違う?適当?に思うのでモーションの再生時間が知りたいです。また参考サイト2はちょっとやり方が違うみたいで参考にならないです。
参考サイト: https://gametukurikata.com/program/continuityattack

イメージ説明

using UnityEngine; //using System.Collections.Generic; using System.Collections; public class Controller : MonoBehaviour { /* Ik 関係 Vector3 rightFootPos = new Vector3(0, 0, 0); Vector3 leftFootPos = new Vector3(0, 0, 0); Quaternion rightFootRot = new Quaternion(0, 0, 0, 0); Quaternion leftFootRot = new Quaternion(0, 0, 0, 0); bool isRightFootIK = false; bool isLeftFootIK = false; float RightIkWeight = 0; float LeftIkWeight = 0; Vector3 rayPositionOffset = new Vector3(0, 0, 0); float rayRange = 1.0f; float offset = 0.1f; */ private Vector3 vt; private float combo_time = 0; private float input_h; private float input_v; private Vector3 move; private float walk_speed;//移動速度 private Vector3 move_direction;//移動方向 private CharacterController cc; private Animator anim; /*判定*/ private bool isAttack = false; private bool isAttack_start = false; private bool isAttack_end = false; private bool isJump = false; private bool isGround = true; private AnimatorClipInfo[] ClipInfo; public GameObject []effects; // public GameObject p; private GameObject GroundCheck; public Transform LeftHand_IK;// //public GameObject LeftHand_target;// //private Transform LeftHand_IK = LeftHand_target; private readonly int all_effects = 4; float j; private bool []combo = new bool[4]; private float comboTime = 1; // public GameObject groundCheck; // Use this for initialization void Start() { j = 0; move = new Vector3(0, 0, 0); cc = GetComponent<CharacterController>(); anim = GetComponent<Animator>(); walk_speed = 7; ClipInfo = anim.GetCurrentAnimatorClipInfo(0); for (int i = 0; i< all_effects; i++) { combo[i] = false; } GroundCheck = transform.Find("GroundCheck").gameObject; GroundCheck.GetComponent<Ground>().isGround = false; } void Move_Mng() { input_h = Input.GetAxis("Horizontal"); input_v = Input.GetAxis("Vertical"); Vector3 move_z = new Vector3(); Vector3 move_x = new Vector3(); move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v; move_x = Camera.main.transform.right * input_h; move_direction = move_x + move_z; move_direction.y = 0; move.x = move_direction.x * walk_speed; move.z = move_direction.z * walk_speed; // Debug.Log("move.x: \n" + move.x); // Debug.Log("move.z: \n" + move.z); if (move_direction != Vector3.zero) { move_direction.y = 0; transform.rotation = Quaternion.LookRotation(move_direction.normalized); } /*ジャンプ*/ if(isJump == true) { Debug.Log("aaa"); move.y += 2; if(move.y > 40) { isJump = false; } }else if (GroundCheck.GetComponent<Ground>().isGround == false && isJump == false) { move.y = - 20; } } void Animator_Mng() { anim.SetFloat("move_forward", getInput()); } float getSpeed() { return Mathf.Abs(Mathf.Sqrt(Mathf.Abs(move.x * move.x) + Mathf.Abs(move.z * move.z))); // return 100;//Mathf.Abs(Mathf.Sqrt( Mathf.Abs(move.x * move.x) + Mathf.Abs(move.z * move.z))); } float getInput() { return Mathf.Abs(Mathf.Sqrt(Mathf.Abs(input_h * input_h) + Mathf.Abs(input_v * input_v))); // return 100;//Mathf.Abs(Mathf.Sqrt( Mathf.Abs(move.x * move.x) + Mathf.Abs(move.z * move.z))); } //IKの重みを設定する void SetIkWeight(AvatarIKGoal _goal, float _posWeight, float _rotWeight) { anim.SetIKPositionWeight(_goal, Mathf.Clamp(_posWeight, 0.5f, 1.0f)); anim.SetIKRotationWeight(_goal, Mathf.Clamp(_rotWeight, 0.5f, 1.0f)); } //IKを指定して固定させる void SetIk(AvatarIKGoal goal, Vector3 _pos, Quaternion _rot, bool isAddIKPosition = false) { if (isAddIKPosition) _pos += anim.GetIKPosition(goal); anim.SetIKPosition(goal, _pos); anim.SetIKRotation(goal, _rot); } IEnumerator combo_corutine_end() { yield return new WaitForSeconds(0.5f); for (int i = 0; i < all_effects; i++) { combo[i] = false; } } IEnumerator combo_corutine(int number) { Instantiate(effects[number], transform.position, transform.rotation); anim.SetTrigger("attackCombo_1"); yield return new WaitForSeconds(0.5f); combo[number] = false; } // Update is called once per frame void Update() { ClipInfo = anim.GetCurrentAnimatorClipInfo(0); /*ジャンプ処理*/ if ( Input.GetKey(KeyCode.Space) == true )//&& GroundCheck.GetComponent<Ground>().isGround == true && isJump == false) { Debug.Log("Jump"); // anim.SetTrigger("jump"); isJump = true; }else //if(isJump == true && GroundCheck.GetComponent<Ground>().isGround == false) { } if (Input.GetKeyDown(KeyCode.Z) == true ) //&& (ClipInfo[0].clip.name == "EllenIdle") ) { isAttack = true; combo[0] = true; }else { isAttack = false; } if (Input.GetKey(KeyCode.LeftShift) == true || Input.GetKey(KeyCode.RightShift) == true) { anim.SetBool("Run_shift", true); } else { anim.SetBool("Run_shift", false); } } ////////////////////////////////////////////////////////////////////////////////////////////// void FixedUpdate() { Move_Mng(); Animator_Mng(); cc.Move(move * Time.deltaTime); } ////////////////////////////////////////////////////////////////////////////////////////////// private void OnAnimatorIK() { Debug.Log("aaa"); anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1); anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1); anim.SetIKPosition(AvatarIKGoal.LeftHand, LeftHand_IK.position); anim.SetIKRotation(AvatarIKGoal.LeftHand, LeftHand_IK.rotation); /* //右手を固定 if (LeftHand_IK != null) { SetIkWeight(AvatarIKGoal.LeftHand, 1.0f, 1.0f); SetIk(AvatarIKGoal.LeftHand, LeftHand_IK.position, LeftHand_IK.rotation); } */ } void MeleeAttackStart() { } void MeleeAttackEnd() { } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

現在のアニメーション時間は
anim.GetCurrentAnimatorStateInfo(0).normalizedTime
で取得できます。normalizedなので0から1の値になります。

現在どのアニメーションが再生されているか(どのステートにいるか)は
anim.GetCurrentAnimatorStateInfo(0).IsName("State名")
がtrueかfalseかで判断できます。

コルーチンでコンボを繋げるなら例えばこんな感じです。

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Teratail : MonoBehaviour 6{ 7 static readonly int NumCombo = 2;//最大コンボ数 8 Animator animator; 9 bool isAttack = false;//攻撃中かどうか 10 11 void Start() 12 { 13 animator = GetComponent<Animator>(); 14 } 15 16 void Update() 17 { 18 if (Input.GetKeyDown(KeyCode.Z) && !isAttack)//攻撃中でないときにZキーが押された 19 { 20 StartCoroutine(Combo());//コンボスタート 21 } 22 } 23 24 IEnumerator Combo() 25 { 26 isAttack = true; 27 28 bool nextCombo = true;//コンボが繋がるかどうか 29 for (int i = 1; nextCombo && i <= NumCombo; i++) 30 { 31 nextCombo = false; 32 animator.SetTrigger("NextCombo");//i番目のコンボ開始 33 yield return new WaitForSeconds(0.1f); 34 while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1f)//コンボiの間 35 { 36 if (Input.GetKeyDown(KeyCode.Z)) nextCombo = true;//キーが押されたらコンボが繋がる 37 yield return null; 38 } 39 yield return new WaitForSeconds(0.3f);//次の攻撃は0.3秒後 40 } 41 42 animator.SetTrigger("EndCombo"); 43 isAttack = false; 44 } 45}

アニメーターはこうなります。
イメージ説明

投稿2020/03/29 04:08

編集2020/03/29 10:24
退会済みユーザー

退会済みユーザー

総合スコア0

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

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

退会済みユーザー

退会済みユーザー

2020/03/29 04:20

コルーチンを使ってアニメイベントを使って終わって0.3秒後くらいにアニメーションを始めるっていう手法はどうなのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問