Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
-1
Question by shadowpuppet · Sep 03, 2016 at 05:41 PM · arrayanimationsrandom.range

still need help with animation array

the script is such that when the enemy walks into a trigger zone, it disables his attack script ( that works) and then is SUPPOSE to play a random assortment of animations from my array. As it is....his attack script does disable and he will randomly play ONE of the animations in my array then STOP. I want him to play a random animation, and when it is finished play another from the array. Anybody help me on this? I have sound array scripts that work and I can't see why this doesn't. I have also tried it with OnTriggerStay but what that does is pick a random animation but play the same animation over and over

 using UnityEngine;
 using System.Collections;
 
 public class AnimationArray : MonoBehaviour {
     private MonoBehaviour Attack;
     public AnimationClip[] clips;
 
     void Start () {
         Attack = GetComponent<EnemyBehaviourDestruct>();
         animation.clip = clips[Random.Range(0, clips.Length)];
     }
 
     
     void OnTriggerEnter(Collider other) {
         if(other.tag == "worker1")
         {
             Attack.enabled = false;
             //animation.clip = clips[Random.Range(0, clips.Length)];
             animation.Play();
         }
     
 }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Alseer7 · Aug 15, 2021 at 12:26 PM

@shadowpuppet

You have animation.clip = clips[Random.Range(0, clips.Length)]; in your Start method, which is only going to run once, hence the single play animation. I would add another If statement inside of your first if method:

 using UnityEngine;
  using System.Collections;
  
  public class AnimationArray : MonoBehaviour {
      private MonoBehaviour Attack;
      public AnimationClip[] clips;
      
  
      void Start () {
          Attack = GetComponent<EnemyBehaviourDestruct>();
          //animation.clip = clips[Random.Range(0, clips.Length)];
      }
  
      
      void OnTriggerStay(Collider other) {
          if(other.tag == "worker1")
          {
              Attack.enabled = false;
              if(!animation.isPlaying)
              {
                  
                  animation.clip = clips[Random.Range(0, clips.Length)];
                  animation.Play();
              }
          }
      
  }
  }


This will (hopefully) make it so that it checks if you are presently playing an animation, if you are, it does nothing and runs the OneTriggerStay method again until it finally can play another animation, and it runs it back through the animation randomizer and then plays the newly selected animation. I hope this helps you out, man!

Comment
Add comment · Share
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
avatar image
0

Answer by junk_rat_angel · Aug 14, 2021 at 04:28 AM

I think you probably want to call Play as many times as the number of clips you play and change the clip it plays each time, so in onTriggerEnter you probably want to set a state which you can reset once all the clips are over and as long as that state is active i.e. clipCity = true,1, etc. (ill leave you to decide how you wanna handle that) you can call a function to handle their queuing with something like: if (anim.isPlaying) { return; } else { animation.clip = clips[Random.Range(0, clips.Length)]; animation.Play(); }

Comment
Add comment · Share
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

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

74 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

Related Questions

How to change a game objects tag after it has been randomly selected? 0 Answers

is it possible toggle bool at editor by script? 1 Answer

Why do I keep getting 'The object you want to instantiate is null' warning? 1 Answer

Choose random object from array not the same as the randomly chosen object before 1 Answer

C# script, light in array randomic 0 Answers


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