Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MattyMidori · Feb 04, 2015 at 11:42 AM · c#2dscripting problemanimatoranimator controller

Having some issues with my Animator Controller.

So I'm working on an Over world for my Turn based Role Playing Game, and it's going kind of well, but I've hit a roadblock at the animations phase. So I have 4 different animations, a frontIdle, backIdle, frontWalk, and backWalk. Here's my script for the animator Controller. The issue i'm having is that it just won't connect I guess? I have the script linked to my player object, along with the animator Controller, but whenever I test it, he just kind of floats around in his idle animation, which is the sprite renderer sprite as well. please keep in mind this is my first submission to the forums as well as my first attempt at a game in general, so some soft criticisms would be appreciated!

 public class PlayerControllerOverworld : MonoBehaviour 
 {
     
     public float walkSpeed = 1; // player left right walk speed
     private bool _isFront = true; //check to see if player is facing front
     
     Animator animator;
     
     //some flags to check when certain animations are playing
 
     //bool _isPlaying_walk = false;
 
     
     //animation states - the values in the animator conditions
     const int STATE_IDLE = 0;
     const int STATE_WALK = 1;
     const int STATE_BACKWALK = 2;
     const int STATE_BACKIDLE = 3;
 
 
     const int FRONT = 0;
     const int BACK = 1;
 
     
     string _currentDirection = "right";
     int _currentAnimationState = STATE_IDLE;
     int _currentFrontOrBack = FRONT;
     // Use this for initialization
     void Start () 
     {
         //define the animator attached to the player
         animator = this.GetComponent<Animator>();
     }
     
     // FixedUpdate is used insead of Update to better handle the physics based jump
     void FixedUpdate()
     {
         //Check for keyboard input
         if (Input.GetKey ("right"))
         {
             changeDirection ("right");
             transform.Translate(Vector3.right * walkSpeed * Time.deltaTime * 3);
             
             if(_isFront == true)
                 changeState(STATE_WALK);
             else if(_isFront == false)
                 changeState(STATE_BACKWALK);
             
         }
 
         else if (Input.GetKey ("left"))
         {
             changeDirection ("left");
             transform.Translate(Vector3.right * walkSpeed * Time.deltaTime * 3);
             
             if(_isFront == true)
                 changeState(STATE_WALK);
             else if(_isFront == false)
                 changeState(STATE_BACKWALK);
             
         }
 
         else if (Input.GetKey ("up"))
         {
             transform.Translate(Vector3.up * walkSpeed * Time.deltaTime * 3);
             _isFront = false;
 
             if(_isFront == false)
                     changeState(STATE_BACKWALK);
         }
 
         else if (Input.GetKey ("down"))
         {
             transform.Translate(Vector3.down * walkSpeed * Time.deltaTime * 3);
             _isFront = true;
             if(_isFront == true)
                 changeState(STATE_WALK);
         }
 
         else
         {
             if(_isFront == true)
                 changeState(STATE_IDLE);
             else if (_isFront == false)
                 changeState(STATE_BACKIDLE);
         }
     }
 
 
     //--------------------------------------
     // Change the players animation state
     //--------------------------------------
     void changeState(int state){
         
         if (_currentAnimationState == state)
             return;
         
         switch (state) {
             
         case STATE_WALK:
             animator.SetInteger ("state", STATE_WALK);
             break;
 
         case STATE_IDLE:
             animator.SetInteger ("state", STATE_IDLE);
             break;
 
         case STATE_BACKWALK:
             animator.SetInteger ("state", STATE_BACKWALK);
             break;
 
         case STATE_BACKIDLE:
             animator.SetInteger ("state", STATE_BACKIDLE);
             break;
 
             
         }
         
         _currentAnimationState = state;
     }
 
 
     void changeBackOrFront(int backOrFront){
         
         if (_currentFrontOrBack == backOrFront)
             return;
         
         switch (backOrFront) {
             
         case STATE_WALK:
             animator.SetInteger ("backOrFront", FRONT);
             break;
             
         case STATE_IDLE:
             animator.SetInteger ("backOrFront", FRONT);
             break;
             
         case STATE_BACKWALK:
             animator.SetInteger ("backOrFront", BACK);
             break;
             
         case STATE_BACKIDLE:
             animator.SetInteger ("backOrFront", BACK);
             break;
             
             
         }
         
         _currentFrontOrBack = backOrFront;
     }
     
     //--------------------------------------
     // Check if player has collided with the floor
     //--------------------------------------
     void OnCollisionEnter2D(Collision2D coll)
     {
         if (coll.gameObject.name == "Floor")
         {
             _isFront = true;
             changeState(STATE_IDLE);
             
         }
         if (coll.gameObject.name == "WallLeft")
         {
             _isFront = true;
             changeState(STATE_IDLE);
             
         }
         if (coll.gameObject.name == "WallRight")
         {
             _isFront = true;
             changeState(STATE_IDLE);
             
         }
         if (coll.gameObject.name == "Roof")
         {
             _isFront = true;
             changeState(STATE_IDLE);
             
         }
 
         
     }
 
     //--------------------------------------
     // Flip player sprite for left/right walking
     //--------------------------------------
     void changeDirection(string direction)
     {
         
         if (_currentDirection != direction)
         {
             if (direction == "right")
             {
                 transform.Rotate (0, 180, 0);
                 _currentDirection = "right";
             }
             else if (direction == "left")
             {
                 transform.Rotate (0, -180, 0);
                 _currentDirection = "left";
             }
         }
         
     }
 
 
 
 }

Comment
Add comment · Show 4
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
avatar image Mmmpies · Feb 04, 2015 at 11:45 AM 0
Share

If you star the game without $$anonymous$$aximizeOnPlay set you can also highlight your player in the hierarchy and then view the Animator screen whilst playing the game.

Do this and see if your integers are being set correctly. Then report back.

avatar image MattyMidori · Feb 04, 2015 at 01:29 PM 0
Share

yup, it's supposedly playing the animations. the animator controller is switching between them, but nothing is changing in the actual game.

avatar image MattyMidori · Feb 04, 2015 at 02:04 PM 0
Share

Never$$anonymous$$d, thanks a bunch though! The problem was in my actual animations, not the scripting or animator controller.

avatar image Mmmpies · Feb 04, 2015 at 02:18 PM 0
Share

Glad you got it sorted @$$anonymous$$atty$$anonymous$$idori

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

why I have to anim.getComponent in update() function when I had done in Start () function 2 Answers

Is there a new retargetting system for the animation in 5.5? 1 Answer

2D Animation does not start 1 Answer

StateMachineBehaviour Issue when swapping runtimeAnimatorController 0 Answers

Help with animator controllers 1 Answer


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