- Home /
Question by
Qauliot · Jul 20, 2016 at 08:51 PM ·
animation2d-platformer
Randomly switch between animations (2D)
I have 2 idle animations. One where the player's hair sways while another has the player swaying his arms a little. However the arm-swinging animation occurs in a loop, after the hair animation to the point where it appears unnatural. However I couldn't find a way to make Unity randomly play the swinging arm animation when idle. So far my code is...`
public float maxSpeed = 10f; //maxWalk speed
bool faceLeft = true; //check to flip animations
public float Speed= 0.0f;
public float move = 0.0f;
public float RandomMove = 0.0f;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
// FixedUpdate is called at constant rate
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");
float RandomMove = Random.Range (0,5);
anim.SetFloat ("Speed", Mathf.Abs (move));
anim.SetFloat ("RandomMove", RandomMove);
GetComponent<Rigidbody2D>().velocity = new Vector2(move*maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move < 0 && !faceLeft)
Flip ();
else if (move > 0 && faceLeft)
Flip ();
}
void Flip () {
faceLeft = !faceLeft;
Vector3 Scale = transform.localScale;
Scale.x *= -1;
transform.localScale = Scale;
}
But the random number generator doesn't change the parameters in the animator...
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612085214im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animator and Scripting problems. Help !! 1 Answer
Moving BoxCollider2d with animation is not the right way? 2 Answers
Animator Condition Not Working 0 Answers
Can the animation editor create local rotational data? 3 Answers
animation state is stuck 0 Answers