Hi,
I have a problem with my blend tree. When my enemy is spawned, i have a code that moves the enemy towards my player.
using Pathfinding;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class EnemyAi : MonoBehaviour
{
private Transform player;
Path path;
int currentWaypoint = 0;
bool reachedEndOfPath = false;
public float speed = 200f;
public float nextWaypointDistance = 3f;
private Animator animator;
private Vector2 moveDirection;
Seeker seeker;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
seeker = GetComponent<Seeker>();
player = GameObject.FindWithTag("Player").transform;
rb = GetComponent<Rigidbody2D>();
animator = GetComponentInChildren<Animator>();
InvokeRepeating("UpdatePath", 0f, .5f);
}
private void UpdatePath() {
if (seeker.IsDone())
{
seeker.StartPath(rb.position, player.position, OnPathComplete);
}
}
private void OnPathComplete(Path p)
{
if (!p.error) {
path = p;
currentWaypoint = 0;
}
}
private void FixedUpdate()
{
if (path == null) return;
if (currentWaypoint >= path.vectorPath.Count)
{
reachedEndOfPath = true;
return;
}
else {
reachedEndOfPath = false;
}
Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
moveDirection = direction;
Animate();
Vector2 force = direction * speed * Time.deltaTime;
rb.AddForce(force);
float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
if (distance <= nextWaypointDistance) {
currentWaypoint++;
}
}
// Update is called once per frame
void Update()
{
}
private void Animate()
{
animator.SetFloat("velocityX", moveDirection.x);
animator.SetFloat("velocityY", moveDirection.y);
}
}
I set the parameters for my blend tree, but it looks like every second my blend tree resets to first animation for like a flicker and then plays animation correctly. Here is a gif of this happening. Why is this flickering happening?
remarkableappropriatehydra