Search Unity

Animation Fix

Discussion in 'Scripting' started by rennster200, Jul 17, 2019.

  1. rennster200

    rennster200

    Joined:
    Jun 21, 2019
    Posts:
    65
    So I created a script where my dog follows me....And the problem is animations...I have animation component on my dog game object and when I move inside the non-following range he plays Idle animation... but when i go outside the range he starts to follow me but animation is not working... Its like he is tiggling left and right for a few inches constantly


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class pet : MonoBehaviour
    5. {
    6.  
    7.     public float speed;
    8.     public float range;
    9.     public Transform player;
    10.     public CharacterController controller;
    11.     public Animation move;
    12.     public Animation Idle;
    13.  
    14.     private void Start()
    15.     {
    16.         move = GetComponent<Animation>();
    17.         Idle = GetComponent<Animation>();
    18.     }
    19.     void Update()
    20.     {
    21.         if (!inRange())
    22.         {
    23.            
    24.             chase();
    25.         }
    26.         else
    27.         {
    28.            
    29.         }
    30.     }
    31.     bool inRange()
    32.     {
    33.         Idle.Play("idleLookAround");
    34.         return Vector3.Distance(transform.position, player.position) < range;
    35.  
    36.     }
    37.     void chase()
    38.     {
    39.         Idle.Stop("idleLookAround");
    40.         move.Play("walk");
    41.  
    42.         transform.LookAt(player.position);
    43.         controller.SimpleMove(transform.forward * speed);
    44.        
    45.     }
    46. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Your inRange() function also contains a Play() call. I don't think you want line 33 there. More likely you want the contents of line 33 on line 28.
     
  3. rennster200

    rennster200

    Joined:
    Jun 21, 2019
    Posts:
    65
    Thanks for that... the thing was also with AI movement speed...speed was equal sort of...but now when he is slower than me when he stops transition from walk to idle animation is not there...I know transitions with animator controller but in scripting not... any tips?