Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

animation problem on character movement

Discussion in 'Scripting' started by Abdul hakim, Aug 27, 2014.

  1. Abdul hakim

    Abdul hakim

    Joined:
    Oct 17, 2013
    Posts:
    39
    I atached these script to the player .
    And added animations.
    And it moving perfectly, but the problem is when i click play,
    it playing the moveP animation automatically.
    How can i make it. If i moving only then play the MoveP animation else play IdleP animation,

    I am actually verry new on this


    Here is the script :

    Code (csharp):
    1. using UnityEngine ;
    2. using System. Collections ;
    3. public class Move : MonoBehaviour {
    4.    
    5.     //movement variable
    6.     public float speed ;
    7.     public float jumpSpeed ;
    8.     public float gravity ;
    9.     private Vector3 moveDirection = Vector3.zero ;
    10.     private CharacterController controller ;
    11.     public float jumpInterval = 5 ;
    12.     private float nextJump = 0 ;
    13.    
    14.    
    15.     // Animation variable
    16.    
    17.     public AnimationClip moveP;
    18.     public AnimationClip IdleP;
    19.     public AnimationClip Jump;
    20.    
    21.    
    22.     void Awake (){
    23.         controller = GetComponent<CharacterController>();
    24.     }
    25.    
    26.     void Update (){
    27.         movePosition();
    28.        
    29.     }
    30.     void movePosition(){       
    31.         if (controller.isGrounded ){           
    32.             moveDirection = new Vector3 (Input.GetAxis("Horizontal"), 0 , 0);      
    33.             moveDirection *= speed ;
    34.             animation.Play (moveP.name);
    35.             if (Input.GetButton("Jump")&&Time.time>nextJump )
    36.             {
    37.                 moveDirection.y = jumpSpeed ;
    38.                 nextJump = Time.time + jumpInterval ;
    39.                 animation.Play (Jump.name);
    40.             }          
    41.         }else{
    42.            animation. Play(IdleP.name);
    43.       }
    44.    
    45.            
    46.         moveDirection.y -= gravity * Time.deltaTime ;
    47.         controller.Move(moveDirection * Time.deltaTime);           
    48.     }
    49. }
    50.  
     
  2. Abdul hakim

    Abdul hakim

    Joined:
    Oct 17, 2013
    Posts:
    39
    any Idea?