- Home /
Zombie AI chasing
hello, i'm using script from youtube videos https://www.youtube.com/watch?v=gXpi1czz5NA, and the script is: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyAI : MonoBehaviour {
public Transform player;
public Animator anim;
public float moveSpeed = 3f;
public int sightRange = 20;
public int minDist = 2;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
transform.LookAt(player);
var direction = player.position - transform.position;
var angle = Vector3.Angle(direction, this.transform.forward);
if (angle < 180 && direction.magnitude < sightRange)
{
var rotation = Quaternion.Slerp(
this.transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime);
rotation.x = 0;
rotation.z = 0;
transform.rotation = rotation;
anim.SetBool("walk", true);
if (direction.magnitude > minDist)
{
transform.Translate(0, 0, Time.deltaTime * moveSpeed);
DisableAnim();
}
}
}
void DisableAnim()
{
anim.SetBool("walk", false);
}
}
why i always get an error like:
KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[System.String,UnityEditor.Graphs.AnimationStateMachine.EdgeInfo].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) UnityEditor.Graphs.AnimationStateMachine.Graph.GetEdgeInfo (UnityEditor.Graphs.Edge edge) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/AnimationStateMachine/Graph.cs:282) UnityEditor.Graphs.AnimationStateMachine.GraphGUI.SyncGraphToUnitySelection (Boolean force) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/AnimationStateMachine/GraphGUI.cs:286) UnityEditor.Graphs.AnimatorControllerTool.DetectAnimatorControllerFromSelection () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Animation/AnimatorControllerTool.cs:392) UnityEditor.Graphs.AnimatorControllerTool.OnEnable () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Animation/AnimatorControllerTool.cs:402)
pls help me, Thanks b4
Answer by GamerJoHo · Jul 10, 2017 at 02:04 PM
It looks like you can't find "walk" in your animator. In the video around 13:55, she set's a bool parameter "isWalking". You need to make sure that it matches PERFECTLY to the script. So if you want to use "walk" in your script, ensure your parameter in the animator is "walk" too, even to the same capital letters.