Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tadpolili · Jun 26, 2016 at 06:01 AM · animatormovement scriptanimator controllernpc

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.

playercontroller.txt (2.6 kB)
npcmovement.txt (4.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to handle multiple animation variants 1 Answer

Animator parameters 1 Answer

Strange index < m_IntCount error using unity2D 4 Answers

Character Animator controller/movement 1 Answer

Animator component prevents NavMeshAgent from disabling/enabling dynamically 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges