- Home /
 
SmoothMoves C# Animation Problem
Hi,
I am new to unity (and coding in general) and have been having some issues with getting one of my animations to play. Here is a section of my code and I will explain what is happening after...
void Update () {
     CharacterController player = GetComponent < CharacterController > ();
 
     if (player.isGrounded)
     {
     velocity = new Vector3 ( Input.GetAxis ( "Horizontal" ),0,0);
         
     RunningConditionsAndAnimation();
     CrouchingConditionsAndAnimation();    
     }
     velocity.y -= gravity * Time.deltaTime;
     player.Move (velocity * Time.deltaTime);    
 
               }
     void RunningConditionsAndAnimation ()
     {
         if (velocity.x > 0)
         {
             velocity.x = runSpeed;
             explorerAnimation.Play ( "Run" );    
             print ( "Run Right");
         }
         if (velocity.x < 0)
         {
             velocity.x = -runSpeed;
             explorerAnimation.Play ( "Run" );    
             print ("Run Left");
         }
     }
         
 
               void CrouchingConditionsAndAnimation ()
{
         if (velocity.x == 0 && Input.GetAxis ( "Vertical" ) < 0)
         {
             explorerAnimation.Play ( "Crouch" );    
             print ("Crouch");
         }
 
               }
If I press the down arrow, the console will print "Crouch" but my crouching animation will not play. Also, if I put any other line of code that is not an animation in CrouchingConditionsAndAnimations() it will work just find. I know there is nothing wrong with my crouch animation because if I replace "Run" with "Crouch" in RunningConditionsAndAnimations(), it plays. This is all based on the Walker Boy's Mario 2D tutorials so maybe I did something wrong turning the javascript into C#. I have spent some time looking for an answer online and have rewritten my code many times but I cannot get it to work. Any help would be appreciated.
I'm not sure what explorerAnimation.Play is and I could not find it in the documentation anywhere, however you could try Animation.play
http://docs.unity3d.com/Documentation/ScriptReference/Animation.Play.html
Hi Ben,
Thanks for pointing that out. What i should have included is that in my variables i have...
public BoneAnimation explorerAnimation;
So, explorerAnimation.Play is the same as animation.Play. I still cannot figure out the problem. It is as if the animation wants to play but something is preventing it.
Why not add the animation component to the object and call the animation from the script attached to the object?
Why call them from another script?
Your answer
 
             Follow this Question
Related Questions
In Game Animation 2 Answers
animation.isPlaying always true and Animation stuck at time 0 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Problem to play animation in C# 5 Answers