- Home /
This question was
closed Mar 03, 2017 at 05:35 AM by
Asellio.
4 directional animation ai 2d
im trying to get my ai to play the correct animation when they move, so if they move left they play the walking left animation. I have looked all over the internet for over a month now and have gotten nothing. here is my script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
public Transform player;
public float moveSpeed;
public bool canMove; //whether ai is allowed to move or not
public float maxDistance;
public float minDistance;
public float attackTime;
public Vector2 lastMove;
public AnimationClip walking;
private bool enemyMoving;
private Animator anim;
private Rigidbody2D myRigidbody;
private float currentMoveSpeed;
private float attackTimeCounter;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
canMove = true;
}
void Update()
{
if (Time.timeScale == 0f)
{
return;
}
if ((Vector2.Distance(transform.position, player.position) < maxDistance)
&& (Vector2.Distance(transform.position, player.position) > minDistance))
{
transform.position = Vector2.MoveTowards(transform.position, player.position, moveSpeed * Time.deltaTime);
}
anim.SetFloat("MoveX", myRigidbody.velocity.x);
anim.SetFloat("MoveY", myRigidbody.velocity.y);
anim.SetBool("EnemyMoving", enemyMoving);
anim.SetFloat("LastMoveX", lastMove.x);
anim.SetFloat("LastMoveY", lastMove.y);
}
}
Comment
Follow this Question
Related Questions
Making a first person rpg enemy AI 2 Answers
2D Animation does not start 1 Answer
2D Undertale like Cutscenes / Animations,2D Undertale type cutscenes / animations. 0 Answers
How to prevent holding down a key to infinitely loop an action? 1 Answer
UI animation when car collides with the border of the track 1 Answer