- Home /
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 :)
UA is not meant for "write a script for me" questions.
Please head to the forums for that.
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;
}
}
}
}
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 :)
Good, glad to help. If you don't $$anonymous$$d, and this helped you, tick my answer correct, to close the question.
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 :)
Try changing
protected int totalAnims = 10;
to
int totalAnims = 10;
protected might not let you use it as a variable operator.
$$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).