Search Unity

Why does my Animals fly?

Discussion in 'Scripting' started by grka, Mar 28, 2015.

  1. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    I have bought an asset in the asset store which worked great till Unity 4.6 But since I changed to 5 my animals start to float over the ground after some seconds and then after a while even rotating around the y axis. I wrote the developer but so far I got no answer since a longer time. So I guess I have to fix that by myself but I don't see the problem. The animal has a character controler, an animation and a FSM.

    The problem seems to starts when the animal changed from the idle state to the moving state. In Unity 4.6 everything was fine

    Code (CSharp):
    1. public class FSM : MonoBehaviour {
    2.     public float gravity = 100.0f;//gravity affecting character
    3.     private enum State {//enumeration of states
    4.         Idle,//idling state
    5.         Search,//searching state
    6.         BackHome,
    7.     }  
    8.     private float speed = 6.0f;//character's movement speed
    9.     private float timer = 1f;//time we're going to spend searching player
    10.     private float timeCheck;//
    11.     private float idlingTime = 5f;//time we're going to spend for idling
    12.     public float idleTimeReset = 5f;//idling time reseter
    13.     private bool alive= true;//cow is still alive
    14.     private bool idling;
    15.     public bool iamACow;
    16.  
    17.  
    18.     private State state;
    19.     private CharacterController charContrl;//character controller
    20.     private Vector3 target;//coordinates of the target
    21.     private Vector3 homePosition;
    22.     private Vector3 moveDirection;//movement direction
    23.     private Vector3 lookDirection;//look direction
    24.  
    25.     // Use this for initialization
    26.     void Awake()
    27.     {
    28.         homePosition = this.transform.position;
    29.     }
    30.  
    31.  
    32.  
    33.     void Start ()
    34.     {
    35.  
    36. ///////CACHING VARIABLES
    37.         timeCheck = Time.time;
    38.         charContrl = GetComponent<CharacterController>();
    39.         state = FSM.State.Search;//starting state set to idle
    40.         StartCoroutine("FSMach");//start our state machine
    41.                
    42.     }
    43.     private IEnumerator FSMach(){
    44.         while(alive){//if character is still alive
    45.             switch(state){
    46.                 case State.Search:
    47.                     Search();
    48.                     break;
    49.                 case State.Idle:
    50.                     Idle();
    51.                     break;
    52.                 case State.BackHome:
    53.                     BackHome();
    54.                     break;
    55.             }
    56.         yield return null;
    57.         }
    58.     }
    59.  
    60.     private void Search(){//search state
    61.         speed = 2f; //current movement speed
    62.         Moving();//run movement function
    63.         if((homePosition - this.transform.position).magnitude >10f){//if we're far from home lets get back there
    64.             idling = false;
    65.             state = FSM.State.BackHome;  
    66.         }      
    67.         if(Time.time - timeCheck > Random.Range(1, 5)){ //if 5 sec. passed
    68.             Vector2 newPosition =(Random.insideUnitSphere * 10f); //calculating random position inside UnitSphere with radius 15
    69.             target = new Vector3(newPosition.x, target.y, newPosition.y)+this.transform.position; //current target's position = random position
    70.             timeCheck = Time.time;//time check equal to absolute time
    71.             timer++;//increase timer by 1
    72.             }
    73.             if(timer>Random.Range(1, 10)){//while timer is lesser than 5
    74.                 state = FSM.State.Idle; // changing state to patrol
    75.                 timer = 0;//zeroing timer
    76.                 idling = true;
    77.                 }
    78.             }
    79.    
    80.     private void Idle(){//idle state
    81.  
    82.             if(idling){
    83.                 if (idlingTime > 0)//while idling time is greater than 0
    84.                     idlingTime -= Time.deltaTime;//descrease idling time by Time.deltaTime
    85.                 if (idlingTime < 0)
    86.                     idlingTime = 0;//idling time can not be lesser than 0
    87.                 if (idlingTime == 0)//when idling time become 0
    88.                     {
    89.                         idling = false;////we're not idling anymore
    90.                         state = FSM.State.Search;//we're patroling
    91.                         idlingTime = idleTimeReset;//reset our idling timer
    92.                         speed = 2f;//change movement speed from 0 to 2
    93.                     }
    94.                 }
    95.         }
    96.     private void BackHome(){//search state
    97.         speed = 2f; //current movement speed
    98.         Moving();//run movement function
    99.         target = homePosition; //current target's position = random position
    100.         Moving();//run movement function
    101.         if((homePosition - this.transform.position).magnitude <= 1f){
    102.         state = FSM.State.Idle; // changing state to patrol
    103.         idling = true;
    104.         }
    105.     }
    106.    
    107.     private void Moving(){//movement function
    108.         if(!idling){//executed when we're not idling
    109.             moveDirection = transform.TransformDirection(Vector3.forward);//moving forward
    110.            
    111.            moveDirection.y -= gravity * Time.deltaTime*10f;// applying gravity
    112.             charContrl.Move(moveDirection *speed *Time.deltaTime); //moving character controller forward with speed over time
    113.             lookDirection = target - this.transform.position;  // calculating look  direction (towards target)
    114.             lookDirection.y = 0; // restricting Y axis rotation
    115.             Quaternion newRot = Quaternion.LookRotation (lookDirection);//rotation that looks along forward with the the head upwards along upwards
    116.             this.transform.rotation = Quaternion.Slerp(transform.rotation, newRot, Time.deltaTime * 3); //smooth linear interpolation from our current rotation to new rotation
    117.         }
    118.     }
    119.     void Update () {
    120.         if(this.GetComponent<Animation>().enabled){//check if animation of character is enabled
    121.         if(speed == 2){//if character's speed is 2
    122.             GetComponent<Animation>().CrossFade("walk");// play walk animation
    123.                 if(iamACow){
    124.                     GetComponent<Animation>()["walk"].speed = 2f;
    125.                 }
    126.             }
    127.             if(idling){//if character's speed is 0
    128.                 GetComponent<Animation>().CrossFade("eat");// play idle animation
    129.                 }
    130.             }
    131.             else{//if animation is disabled - do nothing
    132.                 return;
    133.         }
    134.     }
    135. }
     
  2. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    you are using Animator Component on the Animals.

    Check if Apply Root Motion is checked if yes, disable it.
     
  3. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    Where can I find that? I looked in the inspector and only the dog has an animator component in it. There is a root motion option. All other animals have only an animation and there I can't see that option. Can I find it somewhere else?
     
  4. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    I looked for this root motion and found something. It's disabled. When I enabled root motion then I could see that the movement looks much different from my problem, so I guess the reason is somewhere else. I made a small video to show how my "floating" looks like. It seems as if the animals are stepping on some invisible rock or stair. I can't find a reason.
    http://www.3aussies.de/docs/kuehe.mp4

    The Animationtype is legacy and Generation is "Store in Root (new)"
     
    Last edited: Mar 31, 2015
  5. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    I changed .Move to .SimpleMove and now my animals stay on the ground. But I don't understand why .Move isn't working. In Unity 4.6 it was no problem
     
  6. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    CharacterController.SimpleMove
    public bool SimpleMove(Vector3 speed);

    Description
    Moves the character with speed.

    Velocity along the y-axis is ignored.


    try commenting line 111 (disable it) and run .Move()
    Code (CSharp):
    1. moveDirection.y -= gravity * Time.deltaTime*10f;// applying gravity
    Code (CSharp):
    1. // moveDirection.y -= gravity * Time.deltaTime*10f;// applying gravity
     
  7. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    Thank you this makes them stay down but I don't understand why.