I have an object.
After adding the script, I can change the animation layer using the Space button:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JustPlayer : MonoBehaviour {
public int forceFly = 100;
private Animator myAnimator;
private Rigidbody2D rb;
public bool shield = false;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
myAnimator = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetMouseButtonDown (0)) {
rb.velocity = Vector2.zero;
rb.AddForce (Vector2.down * forceFly);
myAnimator.SetTrigger ("Swim");
}
if (Input.GetKeyDown (KeyCode.Space)) {
StartCoroutine (BubbleSwim ());
}
}
void AnimationControl () {
if (shield == false) {
myAnimator.SetLayerWeight (1, 0);
}
else{
myAnimator.SetLayerWeight (1, 1);
}
}
IEnumerator BubbleSwim ()
{
myAnimator.SetLayerWeight (1, 1);
yield return new WaitForSeconds (10f);
myAnimator.SetLayerWeight (1, 0);
}
}
I would like to change the animation layer using a button that I will add to the screen.
How to do it?