- Home /
This question was
closed Jan 20 at 06:34 PM by
Tronikon for the following reason:
fixed it myself
Question by
Tronikon · Jan 20 at 04:36 AM ·
animationrigidbody2dvelocity2d sprites
My Enemy doesnt switch his animation dependent on rigidbody2d velocity.
i have a 2d enemy that chases the player with a 2d navmesh. There are Sprite animations for going left and right. I made a blend tree for switching between those dependent on the "horizontal" parameter. This Parameter gets its float value from rb.velocity.x. This doesnt work, BUT if the same parameter is assigned to key inputs (the same way the player character animations are done), it does work. So i think the problem lies somewhere in the fact that i use rigidbody.velocity.x for the parameter.
Script:
{
[SerializeField] Transform target;
public Transform enemyGFX;
private NavMeshAgent agent;
private Transform player;
private float dist;
public float howclose;
public GameObject explosion;
public Animator animator;
Vector2 movement;
public Rigidbody2D rb;
private void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
player = GameObject.FindGameObjectWithTag("Player").transform;
agent = GetComponent<NavMeshAgent>();
rb = GetComponent<Rigidbody2D>();
agent.updateRotation = false;
agent.updateUpAxis = false;
}
private void Update()
{
movement.x = rb.velocity.x;
Debug.Log(movement.x);
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Speed", movement.sqrMagnitude);
dist = Vector3.Distance(player.position, transform.position);
if (dist <= howclose)
{
agent.SetDestination(target.position);
Invoke("Explosion", 4);
}
if (dist <= 1.3f)
{
Explosion();
}
}
public void Explosion()
{
Destroy(gameObject);
Instantiate(explosion, transform.position, transform.rotation);
}
}
Comment