Animations in a blend tree.
Hi, Im following a tutorial. It's to have a Npc walk in a random direction stop and repeat. The tutorial then says to add Animations onto it yourself.(As we Covered it in PlayerAnimations) I have it half working.... the npc will use the walk animation when walking and the idle animation when idle. Great! Except its completely ignoring the blend trees and use the two facing down regardless of which direction its facing. I think it has something to do with linking the floats i made in the animator tab which i called "MoveX" "MoveY" "LastMoveX" "LastMoveY" But i have no idea how to link it in. Any help would be Greatly Apprieciated.
Here is my Code so far:
public class VillagerMovement : MonoBehaviour
{
public float moveSpeed;
private Vector2 minWalkPoint;
private Vector2 maxWalkPoint;
private Rigidbody2D myRigidBody;
public bool isWalking;
public float walkTime;
private float walkCounter;
public float waitTime;
private float waitCounter;
private int walkDirection;
public Collider2D walkZone;
private bool hasWalkZone;
private Animator anim;
private bool NpcMoving;
// Start is called before the first frame update
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
waitCounter = waitTime;
walkCounter = walkTime;
ChooseDirection();
if (walkZone != null)
{
minWalkPoint = walkZone.bounds.min;
maxWalkPoint = walkZone.bounds.max;
hasWalkZone = true;
}
}
// Update is called once per frame
void Update()
{
if (isWalking)
{
walkCounter -= Time.deltaTime;
switch (walkDirection)
{
case 0:
myRigidBody.velocity = new Vector2(0, moveSpeed);
if (hasWalkZone && transform.position.y > maxWalkPoint.y)
{
isWalking = false;
waitCounter = waitTime;
NpcMoving = false;
anim.SetBool("NpcMoving", false);
}
break;
case 1:
myRigidBody.velocity = new Vector2(moveSpeed, 0);
if (hasWalkZone && transform.position.x > maxWalkPoint.x)
{
isWalking = false;
waitCounter = waitTime;
NpcMoving = false;
anim.SetBool("NpcMoving", false);
}
break;
case 2:
myRigidBody.velocity = new Vector2(0, -moveSpeed);
if (hasWalkZone && transform.position.y < minWalkPoint.y)
{
isWalking = false;
waitCounter = waitTime;
NpcMoving = false;
anim.SetBool("NpcMoving", false);
}
break;
case 3:
myRigidBody.velocity = new Vector2(-moveSpeed, 0);
if (hasWalkZone && transform.position.x < minWalkPoint.x)
{
isWalking = false;
waitCounter = waitTime;
NpcMoving = false;
anim.SetBool("NpcMoving", false);
}
break;
}
if (walkCounter < 0.33)
{
NpcMoving = false;
anim.SetBool("NpcMoving", false);
}
if (walkCounter < 0)
{
isWalking = false;
waitCounter = waitTime;
}
}
else
{
waitCounter -= Time.deltaTime;
myRigidBody.velocity = Vector2.zero;
if (waitCounter < 0)
{
ChooseDirection();
}
}
}
public void ChooseDirection()
{
walkDirection = Random.Range(0, 4);
isWalking = true;
walkCounter = walkTime;
NpcMoving = true;
anim.SetBool("NpcMoving", NpcMoving);
}
}
Your answer
Follow this Question
Related Questions
How to call Animator from another script 0 Answers
RayCast and Animation not working 0 Answers
Animating Sprites Through Separate Script? 0 Answers
How can i stop my shooting animation after playing? 0 Answers