Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 cheekums89 · Aug 09, 2013 at 10:26 AM · movement script

Idle Randomiser

Hi everybody!

I know this questions has been already solved to a certain extent on another thread, but I was hoping someone might be able to explain it in further detail and help me get a script working. Basically I'm looking to add a script to my character that will play the default idle and then every so often randomise between variant idles. The thing is I'm an animator not a programmer. I understand the means and I can look through script and get a rough idea of what it's doing, but I don't know how to actually write scripts using algorithms and stuff. :)

The thing is it's for a deadline so unfortunately I don't really have the time to sit and learn C# or Java script. I did purchase an Idle Randomiser script from the Asset Store, but I realised afterwards that it's for the Legacy animation system and I'm using Mecanim. I tested it out and it does exactly what I'm wanting except it's for the wrong system.

I know it's a lot to ask, but any help at all would be extremely appreciated.

Thank you :)

Comment
Add comment · Show 2
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 dorpeleg · Aug 09, 2013 at 12:09 PM 1
Share

UA is not meant for "write a script for me" questions.

Please head to the forums for that.

avatar image cheekums89 · Aug 09, 2013 at 06:00 PM 0
Share

Cheers, will do! Sorry I'm kind of new to this

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tw1st3d · Aug 09, 2013 at 06:22 PM

Despite how much I agree with dorpeleg, I understand your position. I'm a programmer, not an animation expert, which gives me a lot of trouble in development. Here's a basic example of how you can do what you want to do, with thorough notes on what I'm doing.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class IdleStance : MonoBehavior
 {
     protected int totalAnims = 10; 
     // Set this to your total number of animations
     
     public Animation firstIdle;
     // Define your original animation
     
     public Animation[] idleAnims = new Animation[totalAnims]; 
     // Create an array of your animations, you can define them in the inspector
     
     public GameObject thePlayer;
     // Define your player in the inspector
     
     protected float walkTime;
     // This will be for a time checker
     
     float waitTime = 5.0f;
     
     protected bool isMoving = false, hasPlayed = false;
     // Create false bools for if you're moving and if the first animation has played
     
     public void Update()
     {
         if(thePlayer.rigidbody.velocity.magnitude > 0)
         {
             // If you're moving, set isMoving to true
             isMoving = true;
             // And set walkTime to the current time
             walkTime = Time.time;
         }else{
             // Else set it to || leave it as false
             isMoving = false;
         }
         
         if(!isMoving)
         {
             // If you're not moving,
             if(walkTime - Time.time > waitTime)
             {
                 // And x amount of seconds have passed
                 if(!hasPlayed)
                 {
                     // If this is you first stopping, play the original animation
                     firstIdle.Play();
                     hasPlayed = true;
                 }
                 if(!firstIdle.isPlaying())
                 {
                     // If it's you continuing to wait, get a random animation
                     int randomAnim = random.Next(0, idleAnims.Length);
                     // And play it.
                     Animation toPlay = idleAnims[randomAnim];
                     toPlay.Play();
                 }
             }else{
                 // If you start moving, set your hasPlayed bool to false
                 hasPlayed = false;
             }
         }
     }
 }
Comment
Add comment · Show 8 · 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 cheekums89 · Aug 09, 2013 at 06:44 PM 0
Share

This is fantastic! Thank you so much for your help :D Yeah, it can be a bit daunting when delving into a different area of development. Ordinarily, if I wasn't under a deadline I would have a go at learning some C#. It's great that you've explained each line as well, I actually understand it now! Thanks again :)

avatar image tw1st3d · Aug 11, 2013 at 05:34 PM 0
Share

Good, glad to help. If you don't $$anonymous$$d, and this helped you, tick my answer correct, to close the question.

avatar image cheekums89 · Aug 12, 2013 at 02:56 PM 0
Share

Hi again :)

Sorry for being a pain, but I was wondering if you might be able to help me fix an error that came up when I tried to attach the script to the character in the inspector. I got this error in the console window and I'm not sure what I need to do to fix it.

(13,50) error CS0236: A field initialiser cannot reference the nonstatic field, method, or property 'IdlesStance.totalAnims'

I'm assu$$anonymous$$g it's something to do with the 13th line of code:

public Animation[] idleAnims = new Animation[totalAnims];

but I'm only going on the feedback given in the console window, I don't know what to do with it after that.

Any help in fixing the error would be much appreciated :)

avatar image tw1st3d · Aug 12, 2013 at 05:36 PM 0
Share

Try changing

 protected int totalAnims = 10;

to

 int totalAnims = 10;

protected might not let you use it as a variable operator.

avatar image cheekums89 · Aug 12, 2013 at 08:23 PM 0
Share

$$anonymous$$ade the change to the script, but unfortunately I'm still getting the same error (CS0236)saying it's the same line of code (13,50).

Show more comments

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

16 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

Related Questions

There is a problem with the code, possibly the animator as well... Please help? 1 Answer

Rigidbody Gravity Too Slow 4 Answers

How to make a GUI.box pop up under an if statement (new to programming) 1 Answer

Megaman Legends scripting help. 1 Answer

Spline Work-Around. Joints? 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