I am having some issues setting up A* pro setup correctly. I see a bunch of jittery movement. I also am having issues with getting animations to play. I am not sure i setup everything correctly.
using Pathfinding;
//using System.Diagnostics;
using UnityEngine;
public class EnemyAnimationController : MonoBehaviour
{
public Animator anim; // Reference to the Animator component
public float naturalSpeed = 5f; // The speed at which animations are designed to play
private IAstarAI ai; // Reference to A* Pathfinding AI
private Transform tr; // Reference to the enemy's transform
private const string NormalizedSpeedKey = "NormalizedSpeed";
private static int NormalizedSpeedKeyHash = Animator.StringToHash(NormalizedSpeedKey);
// For debugging and seeing what values are being used in the Inspector
[SerializeField] private float currentVelocity = 0f; // Current velocity for debugging
[SerializeField] private bool isAtTarget = false; // Whether the enemy is at the target for debugging
void Awake()
{
ai = GetComponent<IAstarAI>();
tr = GetComponent<Transform>();
if (anim != null && !HasParameter(anim, NormalizedSpeedKey))
{
Debug.LogError($"No '{NormalizedSpeedKey}' parameter found on the animator. The animator must have a float parameter called '{NormalizedSpeedKey}'", this);
enabled = false;
}
}
static bool HasParameter(Animator animator, string paramName)
{
foreach (AnimatorControllerParameter param in animator.parameters)
{
if (param.name == paramName)
return true;
}
return false;
}
void Update()
{
if (ai == null) return;
// Calculate if the enemy has reached the target
if (ai.reachedEndOfPath)
{
if (!isAtTarget)
{
// Only update when target is reached to avoid repeated checks
OnTargetReached();
}
isAtTarget = true;
}
else
{
isAtTarget = false;
}
// Calculate velocity relative to this transform's orientation
Vector3 relVelocity = tr.InverseTransformDirection(ai.velocity);
relVelocity.y = 0;
// Expose the velocity to the Inspector for debugging
currentVelocity = relVelocity.magnitude;
// Set the NormalizedSpeed parameter to control animation speed
anim.SetFloat(NormalizedSpeedKeyHash, relVelocity.magnitude / (naturalSpeed * anim.transform.lossyScale.x));
// Debugging
Debug.Log($"Velocity: {relVelocity.magnitude}, Is At Target: {isAtTarget}");
}
// Called when the end of path has been reached
private void OnTargetReached()
{
// Stop animation or set it to idle
anim.SetFloat(NormalizedSpeedKeyHash, 0f);
Debug.Log("Target Reached. Stopping Animation.");
}
}
For the jittering issue, is there any code affecting the position of the agent outside of Astar? Also can you screenshot/send your graph settings? If it’s not another piece of code I want to try to reproduce it for you to see what the issue may be
I’m assuming NormalizedSpeedKeyHash is linked to a walk animation, where when it’s over 0 it plays? Can’t see the animator component so just want to confirm before looking more deep into it
I may have some mis-understanding on how the process is setup. I thought i needed a navmesh(and I might because without the enemy hits a wall and falls right through the probuilder mesh) but with the navmesh i was having odd issues. I made a new project with unity 2022.3 LTS as with the one with jitters is using unity 6. but I think i had more issues. now I’m having issues with animation still. I am having now an issue with enemy if they get too close to wall(probuilder) he falls out.
If needs be, i can make a unity package which includes everything except fps engine, behavior designer, and a*
I will try and provide anything needed to explain my issue.
Video of enemy not correctly animating and falling off
here is code to try and make animations work.
using UnityEngine;
using Pathfinding;
public class EnemyAnimation : MonoBehaviour
{
public Animator animator;
public AIPath aiPath;
void Update()
{
// Get the velocity from the AIPath component
Vector3 velocity = aiPath.velocity;
// Update animation parameters
animator.SetFloat("VelocityX", velocity.x);
animator.SetFloat("VelocityY", velocity.y);
// Optional: Debug line to visualize velocity
Debug.DrawLine(transform.position, transform.position + velocity, Color.red);
}
}
Oh wow you’ve been busy man, impressive! I always love seeing this kinda stuff. For hugging the wall, you’re gonna wanna take a look at modifiers- namely the radius and funnel modifiers may be helpful here
animation doesn’t stop when it arrives at the target player. but does stop when it is looking around for player. also certain corners the enemy clips through wall and falls.
Have your AstarPath or AIPath settings changed from your original screenshot, save for the change you made to turning off cut corners? If they’ve changed you can post the new settings and I’ll try to replicate the issue on my end and fix it
I have the following currently. is it possible to join me on screen share discord? I am confused.
is this because i am using pro-builder. this is unity 2022.3.3 LTS
I must be doing something wrong, as i build a new level out of boxes, and some small obstacles, and used a simple AIDestination script, AIPath default, and a grid all default. and still the enemy falls off the map. the enemy and player are simple shapes too. so idk…
noticed i sorta fixed it on new map by using an erosion
i still can get enemy to walk right thought objects marked as obstacle mask.
Taking a look at this now, and there are some scripts missing in the UnityPackage. I’ve added Astar but there are other scripts missing and I’m not sure how the agent gets it’s destination- was that done in the Behavior Trees asset?
its using aiDestrinator Setter. also i just did a quick recap of your tutorial and noticed, maybe my issue is i need a character Controller on the enemy. as i missed that. and set center’s Y to -0.1, i didn’t know these two things. may help some of my issues
oh i see the missing script. that was for animations i forgot to add it. its this…
using UnityEngine;
using Pathfinding;
public class EnemyAnimation : MonoBehaviour
{
public Animator animator;
public AIPath aiPath;
void Update()
{
// Get the velocity from the AIPath component
Vector3 velocity = aiPath.velocity;
// Normalize the velocity to avoid values higher than 1
Vector3 normalizedVelocity = velocity.normalized;
// Update animation parameters
animator.SetFloat("VelocityX", normalizedVelocity.x);
animator.SetFloat("VelocityY", normalizedVelocity.z);
Debug.Log("vel X" + normalizedVelocity.x.ToString() + " vel Z " + normalizedVelocity.z.ToString());
// Optional: Debug line to visualize velocity
Debug.DrawLine(transform.position, transform.position + velocity, Color.red);
}
}
it looks like using recast with richai is working as expected. now i need to see how to add animations using this method as before i used aipath as a way to get velocity or was it magnitude to animate it…
i tried
Vector3 velocity = richAI.velocity;
and normalized it, but didn’t seem to work