- Home /
Animator Override Controller for NPC/ Overriding sprite animations
Hi!
I am currently working on a 2d rpg game. I have created a 'player' who can move about a space using the keyboard arrows (using gamesplusjames tutorial). The player's sprite has a walk animation that works. I used a player movement script and created an animator that uses blend trees for idle and walk animations.
Here's the script for player movement:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private Animator anim;
private bool playerMoving;
public Vector2 lastMove;
private Rigidbody2D myRigidBody;
private static bool playerExists;
public bool canMove;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
myRigidBody = GetComponent<Rigidbody2D>();
if (!playerExists)
{
playerExists = true;
DontDestroyOnLoad(transform.gameObject);
} else
{
Destroy(gameObject);
}
canMove = true;
}
// Update is called once per frame
void Update () {
playerMoving = false;
if (!canMove)
{
myRigidBody.velocity = Vector2.zero;
return;
}
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
//transform.Translate(new Vector3 ( Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f ));
myRigidBody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, myRigidBody.velocity.y);
playerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
//transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, Input.GetAxisRaw("Vertical")* moveSpeed );
playerMoving = true;
lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
if(Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
{
myRigidBody.velocity = new Vector2(0f, myRigidBody.velocity.y);
}
if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, 0f);
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
anim.SetBool("PlayerMoving", playerMoving);
anim.SetFloat("LastMoveX", lastMove.x);
anim.SetFloat("LastMoveY", lastMove.y);
GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1;
}
}
Here is the script for npc movement:
using UnityEngine;
using System.Collections;
public class StudentMovement : 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;
public bool cantMove;
private DialogueManager theDM;
private Animator anim;
public Vector2 lastMove;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
myRigidBody = GetComponent<Rigidbody2D>();
theDM = FindObjectOfType<DialogueManager>();
waitCounter = waitTime;
walkCounter = walkTime;
ChooseDirection();
if (walkZone != null)
{
minWalkPoint = walkZone.bounds.min;
maxWalkPoint = walkZone.bounds.max;
hasWalkZone = true;
}
cantMove = true;
}
// Update is called once per frame
void Update () {
if (!theDM.dialogActive)
{
cantMove = true;
}
if (!cantMove)
{
myRigidBody.velocity = Vector2.zero;
return;
}
if (isWalking)
{
walkCounter -= Time.deltaTime;
if(walkCounter < 0)
{
isWalking = false;
waitCounter = waitTime;
}
switch (WalkDirection)
{
case 0:
//moving the sprite in the aforementioned direction
myRigidBody.velocity = new Vector2(0, moveSpeed);
anim.SetFloat("MoveX", 1);
anim.SetFloat("MoveY", 0);
if (hasWalkZone && transform.position.y > maxWalkPoint.y)
{
isWalking = false;
waitCounter = waitTime;
}
break;
case 1:
anim.SetFloat("MoveX", 0);
anim.SetFloat("MoveY", 1);
myRigidBody.velocity = new Vector2(moveSpeed, 0);
if (hasWalkZone && transform.position.x > maxWalkPoint.x)
{
isWalking = false;
waitCounter = waitTime;
}
break;
case 2:
myRigidBody.velocity = new Vector2(0, -moveSpeed);
anim.SetFloat("MoveX", -1);
anim.SetFloat("MoveY", 0);
if (hasWalkZone && transform.position.y < minWalkPoint.y)
{
isWalking = false;
waitCounter = waitTime;
}
break;
case 3:
myRigidBody.velocity = new Vector2(-moveSpeed, 0);
anim.SetFloat("MoveX", 0);
anim.SetFloat("MoveY", -1);
if (hasWalkZone && transform.position.x < minWalkPoint.x)
{
isWalking = false;
waitCounter = waitTime;
}
break;
}
}
else
{
waitCounter -= Time.deltaTime;
myRigidBody.velocity = Vector2.zero;
if(waitCounter < 0)
{
ChooseDirection();
}
}
GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1;
}
public void ChooseDirection()
{
WalkDirection = Random.Range(0, 4);
isWalking = true;
walkCounter = walkTime;
}
}
Now to the issue, I made an npc that can randomly move around but there are no animations connected to this movement. I thought I could create an animator override controller for all future npcs and base it off of the animator for player movement. I was able to enter the npc related animations in the overrider and connect the animator to my npc but I still do not see any animations.
If someone could assist me or point me in the right direction it would be greatly appreciated.