Search Unity

Unable to Get Model To Use Walking Animation

Discussion in 'Scripting' started by Lluvian, Apr 25, 2015.

  1. Lluvian

    Lluvian

    Joined:
    Apr 19, 2015
    Posts:
    6
    Hello all,

    I am trying to get my Sentrino Necromancer to walk in game using his walking animation. He moves around perfectly with the mouse click script but his walking animation does not work. I get the warning: Parameter'walk2' does not exist. Yet I can see it and I have attached the screenshot. What am I doing wrong?

    Code (CSharp):
    1.  private Transform myTransform;              // this transform
    2.     private Vector3 destinationPosition;        // The destination Point
    3.     private float destinationDistance;          // The distance between myTransform and destinationPosition
    4.     public float Speed;                         // Speed at which the character moves
    5.     public float Direction;                    // The Speed the character will move
    6.     public float moveAnimSpeed;                // Float trigger for Float created in Mecanim (Use this to trigger transitions.
    7.     public float animSpeed = 1.5f;            // Animation Speed
    8.     public Animator anim;                     // Animator to Anim converter
    9.     public int idleState = Animator.StringToHash("Base Layer.Idle"); // String to Hash conversion for Mecanim "Base Layer"
    10.     public int runState = Animator.StringToHash("Base Layer.Run");  // String to Hash for Mecanim "Base Layer Run"
    11.     private AnimatorStateInfo currentBaseState;         // a reference to the current state of the animator, used for base layer
    12.     private Collider col;
    13.  
    14.     public AnimationClip run;
    15.     public AnimationClip idle;
    16.  
    17.  
    18.  
    19.     void Start()
    20.     {
    21.  
    22.         Physics.gravity = new Vector3(0, -200f, 0); // used In conjunction with RigidBody for better Gravity (Freeze Rotation X,Y,Z), set mass and use Gravity)
    23.         anim = GetComponent<Animator>();
    24.         idleState = Animator.StringToHash("Idle"); // Duplicate added due to Bug
    25.         runState = Animator.StringToHash("Run");
    26.         myTransform = transform;                            // sets myTransform to this GameObject.transform
    27.         destinationPosition = myTransform.position;
    28.         // prevents myTransform reset
    29.     }
    30.  
    31.     void FixedUpdate() {
    32.         // keep track of the distance between this gameObject and destinationPosition  
    33.    
    34.         currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
    35.    
    36.         destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
    37.            
    38.                  // Set's speed in reference to distance
    39.  
    40.             if(destinationDistance < .5f){  
    41.             Speed = 0;
    42.         }
    43.         else if(destinationDistance > .5f){    
    44.             Speed = 8;
    45.    
    46.             //Below sends Floats to Mecanim, Raycast set's speed to X until destination is reached animation is played until speed drops
    47.         }
    48.      
    49.         if (Speed > .5f){
    50.             anim.SetFloat("walk2", 2.0f);}        
    51.        
    52.                 else if (Speed < .5f){
    53.                     anim.SetFloat("walk2", 0.0f);} //
    54.         // Moves the Player if the Left Mouse Button was clicked
    55.    
    56.    
    57.         if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0) {
    58.             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
    59.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    60.             float hitdist = 0.0f;
    61.             if (playerPlane.Raycast(ray, out hitdist)) {
    62.                 Vector3 targetPoint = ray.GetPoint(hitdist);
    63.                 destinationPosition = ray.GetPoint(hitdist);
    64.                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
    65.                 myTransform.rotation = targetRotation;
    66.             }
    67.        }
    68.    
    69.  
    70.    
    71.         // Moves the player if the mouse button is hold down
    72.         else if (Input.GetMouseButton(0) && GUIUtility.hotControl ==0) {
    73.             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
    74.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    75.             float hitdist = 0.0f;
    76.             if (playerPlane.Raycast(ray, out hitdist)) {
    77.                 Vector3 targetPoint = ray.GetPoint(hitdist);
    78.                 destinationPosition = ray.GetPoint(hitdist);
    79.                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
    80.                 myTransform.rotation = targetRotation;
    81.             }
    82.         //  myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
    83.         }
    84.         // To prevent code from running if not needed
    85.         if(destinationDistance > .5f){
    86.             myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, Speed * Time.deltaTime);
    87.         }
    Hi guys,

    I am using the Sentrino Necromancer and I am trying to get him to use his walking animation with my script. Can someone tell me what I am doing wrong.
     

    Attached Files:

  2. KingArbinV

    KingArbinV

    Joined:
    Apr 29, 2014
    Posts:
    8
    anim was deprecated with unity 5.0, i assume that is the version of unity you are using right? try something like:

    GameObject<Animation>.SetFloat
     
  3. Lluvian

    Lluvian

    Joined:
    Apr 19, 2015
    Posts:
    6
    Do you know of a tutorial on this? I am struggling on applying these concepts.