- Home /
Question by
SesshogeGames · Dec 11, 2018 at 04:00 PM ·
animation2d gamerpgnpcfollow player
NPC Party Member Direction
So this time, my problem is a small one. In my 2D overhead RPG game, I have a party member following me around, and it works quite well. My only problem is the animator. Because of the way she moves, it was quite tricky to fix her idle animations, and for the most part, it works, but for a split second, she faces forward, which is her default idle animation. Is there anything that can fix this script?
public class PartyMemberFollowController : MonoBehaviour {
public Transform target;
private bool isRunning;
public float currentSpeed;
public float walkSpeed;
public float runSpeed;
public float stoppingDistance;
public Vector2 myLastDirection;
private Animator anim;
private bool NPCMoving;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
NPCMoving = false;
if (Vector2.Distance(transform.position, target.position) > stoppingDistance)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, currentSpeed * Time.deltaTime);
NPCMoving = true;
myLastDirection = GameObject.Find("Player").GetComponent<PlayerController>().lastDirection;
}
if (Input.GetKey(KeyCode.LeftShift))
{
isRunning = true;
currentSpeed = runSpeed;
}
else
{
isRunning = false;
currentSpeed = walkSpeed;
}
myLastDirection = GameObject.Find("Player").GetComponent<PlayerController>().lastDirection;
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
anim.SetFloat("LastMoveX", myLastDirection.x);
anim.SetFloat("LastMoveY", myLastDirection.y);
anim.SetBool("IsMoving", NPCMoving);
}
}
Comment
Best Answer
Answer by SesshogeGames · Dec 12, 2018 at 09:40 PM
It was a small fix. Since I am constantly receiving the direction of the player, I set the 2D Simple Directional variables to LastMoveX and LastMoveY, and everything worked out fine. The animations are now consistent.