I'm a real noob, I am an artist that just started out programming and have looked around a lot for a solution to this, the closest i could find was a reddit post with the same problem as mine but even there no one could find a solution.
I have set up an animator controller and have given it all the parameters i need, walking, running, jumping etc. If I use a player controller script everything works like a charm. Now when I want to turn this character into an NPC and place the patrol AI script I picked up from the unity manual, things aren't working right.
I have baked my navmesh, I have added the NavMeshAgent component too. The NPC slides around, but the animation doesn't play
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class Patrol : MonoBehaviour
{
Animator animator;
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent agent;
void Start()
{
animator = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint()
{
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint 1) % points.Length;
}
void Update()
{
animator.SetBool("isRunning", true);
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
}
}
CodePudding user response:
Animator starts from the default state (indicated by orange color) and transitions happen based on that. Based on the screenshot of your animator, from your default state, animator can go to Idle, SwordSlash and SwordJump states and there is no transition between the Idle state and the Run state. Nevertheless your code sets the isRunning bool to true. If you want it to work you must set the isWalking bool to true, so the animator goes into SwordWalk state. From there you can set isRunning to true to go to Run animation. Another way is to change isRunning to isWalking (By the way, I suggest you study about 1D blend trees)