Question by
dragonkingdom · May 30, 2016 at 02:39 PM ·
animationanimatorattack
Animator not been initialized please help
The animator isn't initialized please help Please give answers with instructions please TY Code:
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour { private Rigidbody2D myRigidBody;
private Animator myAnimator;
[SerializeField]
private float movementSpeed;
private bool attack;
private bool facingRight;
// Use this for initialization
void Start ()
{
facingRight = true;
myRigidBody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
void update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate ()
{
float horizontal = Input.GetAxis("Horizontal");
HandleMovement(horizontal);
Flip(horizontal);
HandleAttacks();
}
private void HandleMovement(float horizontal)
{
myRigidBody.velocity = new Vector2(horizontal * movementSpeed, myRigidBody.velocity.y);
myAnimator.SetFloat("Speed", Mathf.Abs (horizontal));
}
private void HandleAttacks()
{
if (attack)
{
myAnimator.SetTrigger("attack");
}
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
attack = true;
}
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 thescale = transform.localScale;
thescale.x *= -1;
transform.localScale = thescale;
}
}
}
Comment
What do you mean by "Animator not been initialized", please, provide your animator
Your answer
Follow this Question
Related Questions
Range Attack + Animation Script 0 Answers
Applying permanent mask to player animators walk 0 Answers
How do I modify animation parameters from script (C#) 1 Answer
Animator Position VS Script Vector 3 0 Answers
Attack function animator problem 0 Answers