So, i was making this code and it happens to me. I have one NPC on my scene called CatLite, and he’s following me as it should, but he’s not fixed on y axis, he starts walking on the air. If anyone could tell me where i’m missing i would be thankful.
Here’s the entire code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NPCFollow : MonoBehaviour
{
public GameObject Player;
public float TargetDistance;
public float AllowedDistance = 5;
public GameObject CatLite;
public float FollowSpeed;
public RaycastHit Shot;
void Update()
{
transform.LookAt(Player.transform);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance >= AllowedDistance)
{
FollowSpeed = 0.02f;
CatLite.GetComponent<Animator>().Play("Walk");
transform.position = Vector3.MoveTowards(transform.position, Player.transform.position, FollowSpeed);
}
else
{
FollowSpeed = 0;
CatLite.GetComponent<Animator>().Play("Idle_B");
}
}
}
}
btw, i’m using Navigation Static, don’t know if i’m getting it too complex for something that maybe is simple, so that’s it