Here is the script for the animation workshop player character in Bertozzi's animation workshop demo.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControlPlayer : MonoBehaviour {
private Rigidbody2D myPlayerRB;
private Animator myAnimator;
[SerializeField] private int speed;
private bool facingRight;
void Start(){
myPlayerRB = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
facingRight = true;
}
void FixedUpdate(){
float horizontal = Input.GetAxis ("Horizontal");// returns a value between -1 and 1)
//Debug.Log ("horizontal is :" + horizontal);
ControlElf (horizontal);
FlipElf (horizontal);
// set a trigger to cause the jump animation in the animator
if(Input.GetKeyDown(KeyCode.Space)) myAnimator.SetTrigger("jump");
}
void ControlElf(float horizontal){
myPlayerRB.velocity = new Vector2 (horizontal * speed, myPlayerRB.velocity.y);
myAnimator.SetFloat ("elfSpeed", Mathf.Abs (horizontal)); // set horiz to positive and send to animator
}
void FlipElf(float horizontal){
if ((horizontal > 0 && !facingRight) || (horizontal < 0 && facingRight)) {
Vector3 elfScale = transform.localScale; // current pos
elfScale.x *= -1; // reverses pos and neg of x
transform.localScale = elfScale;
facingRight = !facingRight;
}
}
}