Search Unity

Question Lag or suffering in OnStateUpdate

Discussion in 'Scripting' started by Evil-ProHack, Jul 12, 2020.

  1. Evil-ProHack

    Evil-ProHack

    Joined:
    Sep 15, 2017
    Posts:
    7
    I´m trying to throw a arm to the player while the arm is rotating in the animation, but the arm doesn't move correctly in the update

    I do not know what is the problem

    Code (CSharp):
    1.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    2.    {
    3.         timer = Random.Range(minTime, maxTime);
    4.         animator.SetFloat("timelife", timer);
    5.         rightArm = GameObject.Find("RightArm").GetComponent<Transform>();
    6.         rightArmPos = rightArm.localPosition;
    7.         boss = GameObject.Find("Bossu2Real").GetComponent<BossBehaviour2>();
    8.         player = GameObject.Find("nave").GetComponent<PlayerBehaviour>();
    9.         animator.SetBool("isAttackingWithArms", true);
    10.    
    11.         isAtackingWithRightArm = true;
    12.         float lifetime = 0.3f;
    13.    
    14.         direction.Normalize();
    15.         rightArm.transform.position = rightArm.transform.position + direction * armSpeed;
    16.         Debug.Log("posicion" + rightArm.transform.position);
    17.         Debug.Log("Throwing arm");
    18.         animator.SetFloat("timelife", lifetime);
    19.        
    20.  
    21.              
    22.         }
    23.    
    24.  
    25.     // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    26.     override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    27.    {
    28.          direction = rightArm.position - boss.transform.position;
    29.         direction.Normalize();
    30.         rightArm.transform.position = boss.transform.position + direction * armSpeed;
    31.         //rightArm.position = direction * armSpeed * Time.deltaTime;
    32.  
    33.         timer -= Time.deltaTime;
    34.         Debug.Log("Brazo en movimiento// Arm in movement");
    35.  
    36.         if (timer <= 0)
    37.         {
    38.             Debug.Log("He esperado el timer");
    39.             //    Destroy(rightArm.gameObject, lifetime);
    40.             isAtackingWithRightArm = false;
    41.          
    42.             rightArm.localPosition = new Vector3(1.655198f, -1.130143f,0);
    43.             animator.ResetTrigger("isAttackingWithRightArm");
    44.  
    45.             animator.SetTrigger("idle");
    46.        
    47.         }
    48.      
    49.  
    50.      
    51.         }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    The animator is overriding any position changes you're making in your code.
     
  3. Evil-ProHack

    Evil-ProHack

    Joined:
    Sep 15, 2017
    Posts:
    7
    How can I fix it? the rotate animation only modifies the rotation not the position so I know that is not
     
  4. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    i don't know about using OnStateUpdate, but when i'm trying to get around animations resetting rotations I'm trying to make, for stuff like recoil, or having a characters head face another character, I put my code in LateUpdate because it runs after everything else.