Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Piyush_Mishra · Jan 25, 2020 at 10:14 PM · animation controlleranimationstateanimation scriptinputs

,Capture user input as per animation state and then play animations accordingly

Objective

To enable a user to respond to left or right attacks. If attacks are responded in time, then the attack is blocked. Otherwise, if not responded in time or responded in wrong direction (that is attack from left but user pressed right) then user will take a Hit. Essentially, I guess this is what i want:

  1. First, I want to trigger animation events (of left or right attack).

  2. Then, want to capture user input ('A' or 'D')

  3. Then, I wish to check if user input according to attack (for left attack need to press 'A', for right attack 'D' key.

  4. Lastly, play blocked or hit accordingly.

I am very new to Unity, and any help, any link will be very much appreciated.

Process so far:

Made a GameObject. Attached an animator using single frame animation events (basically just images, with no loop time). And attached the following script on the GameObject. Also made my Fixed time update to 1s (in Unity project settings). Following is animator setting {L,R,B,I stand for Left, right, blocked,Idle}

alt text

Problem:

During run, the animation goes to idle first (default state), then immediately to Hit. I tried pressing A (or even D) for multiple FixedUpdates and in some of them even managed to Block (not Hit), but almost always it jumps so quickly to Hit state that I can't react to it.

Also I would not like to use FixedUpdate of 1s, becoz i would like to implement it on animating characters and there time intervals will be much shorter. But using Update method, there are far more runs per second than i can visualise on screen and respond to.

Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     Animator _enemyAnimator;
     [SerializeField] float rxnTime=0.5f;
     int attackLorR;
     
     void Start()
     {
         _enemyAnimator = GetComponent<Animator>();
     }
 
     void Update()
     {

         LorRAttack();
         if (attackLorR == 1)
         { _enemyAnimator.SetTrigger("isLeft"); Debug.Log("LeftTriggered"); }
         else if (attackLorR == 2)
         { _enemyAnimator.SetTrigger("isRight"); Debug.Log("RightTriggered"); }
 
         StartCoroutine(WaitForReaction(rxnTime));
         {
             if (Input.GetKey(KeyCode.A) && attackLorR == 1)
             {
                 _enemyAnimator.SetTrigger("isBlocked");
                 Debug.Log("SUCCESS: Left Attack Blocked");
             }
             else if (Input.GetKey(KeyCode.D) && attackLorR == 2)
             {
                 _enemyAnimator.SetTrigger("isBlocked");
                 Debug.Log("SUCCESS: Right Attack Blocked");
             }
             else if (Input.GetKey(KeyCode.A) && attackLorR == 2)
             {
                 _enemyAnimator.SetTrigger("isHit");
                 Debug.Log("FAILED: Right Attack Missed");
             }
             else if (Input.GetKey(KeyCode.D) && attackLorR == 1)
             {
                 _enemyAnimator.SetTrigger("isHit");
                 Debug.Log("FAILED: Left Attack Missed");
             }
             else if ((!Input.GetKey(KeyCode.D) || !Input.GetKeyDown(KeyCode.D)) &&
                 (attackLorR == 1 || attackLorR == 2))
             {
                 _enemyAnimator.SetTrigger("isHit");
                 Debug.Log("FAILED: NoKeyPress");
             }
         }
         
     }
 
     private void LorRAttack()
     {
         float num = Random.Range(0.0f, 2.0f);
         if (num < 1f) attackLorR = 1;
         else attackLorR = 2;
         Debug.Log(attackLorR);
 
     }
     IEnumerator WaitForReaction(float rxnTime)
     {
         yield return new WaitForSeconds(rxnTime);
     }
 }
 


Thanks a lot for reading this far

capture.png (68.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animation layer naming convention between enemies and player 0 Answers

Animation state not transitioning at 100% 1 Answer

How to reset the animation state to start it again 0 Answers

Idle Animation Won't Play 1 Answer

Play simple animations on demand 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges