Search Unity

Set animation position proprieties with code

Discussion in 'Scripting' started by MikeyJY, Jan 28, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I have an axe chop tree animation with 4 keys based on position and rotation(axe the child of the player). When the player isn't near a tree the position proprieties are constant(relative to player). When the player is near a tree, the animation doesn't stops until the axe reaches the position proprieties and that causes a glitch and the axe is moving through tree!
    I tried to check if player is near tree like this:

    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2.     {
    3.         if(other.transform.gameObject.tag == "Tree")
    4.         {
    5.             nearTree = true;
    6.             Tree = other.transform.gameObject;
    7.         }
    8.     }
    9.     private void OnTriggerExit(Collider other)
    10.     {
    11.         if (other.transform.gameObject.tag == "Tree")
    12.         {
    13.             nearTree = false;
    14.         }
    15.     }
    16. void Update(){
    17.  
    18. if(nearTree==false){
    19. animation.play(hatchet); //the animation starts with constant values
    20. }
    21. if(nearTree==true){
    22. //I want to change the animation position at an exact frame and replace the x, y, z values with Tree.transform.position.x, Tree.transform.position.y, Tree.transform.position.z, so the animation should stop when the axe reaches tree position
    23. }
    24. }
    It checks correctly if the player is near tree!
    I didn't find how to change animation keys on the web!
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You can't change animation keys in runtime, only in Editor scripts. What you want is called Inverse Kinematics (IK). I don't think unity embedded IK solution is capable of this nor I don't know if your player has special bone for the axe. So first check out embedded solution and then look into assets store for commercial/free ones.

    Also it may be you want to add collider to your axe and employ another clip when the axe touches the three and maybe put in some particles.
     
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    [QUOTE="Also it may be you want to add collider to your axe and employ another clip when the axe touches the three and maybe put in some particles.[/QUOTE]

    That means to check if the hatchet is colliding with a tree and if it is true to stop the animation? I tried this but I don't know how to handle this because I have 3 animations:
    axeidle that's play anytime, axeidleneartree that's play when the player is near the tree but doesn't hit the tree and axehit! And I created a code to verify if the player spams the click button, if the interval between clicks is more than 0.5sec, the choptree paramters is deactivated and the transition between axehit and axeidleneartree is played:
    Code (CSharp):
    1. void Update () {
    2.         if (nearTree == true)
    3.         {
    4.             if (Input.GetButtonDown("Fire1"))
    5.             {
    6.            
    7.              
    8.            
    9.                 animator.Play("hatchet");
    10.                
    11.                 numberOfClicksInARow++;
    12.                 timeLastClicked = Time.time;
    13.             }
    14.        
    15.             if (Time.time > timeLastClicked + maxTimeInterval) animator.SetBool("ChopTree", false);
    16.             if (Time.time < timeLastClicked + maxTimeInterval) animator.SetBool("ChopTree", true);
    17.          
    18.         }
    19.  
    20.  
    21.     }

    And my chopping animation has 4 keyframes:
    initial, the final axe position, a pause and back to initial

    And if the axe hits the tree the animation should be skipped the "back to initial" keyframe!
    But I can't figure out all these thing, can you give me an example?
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    This is not correct. When hit, add particle effect to hide from players eye minor mesh intersections and add another after-hit animation clip where your character pulls axe back
     
  5. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    ok but if the player stays in an worng position the axe glitches totally! look at this screen capture:
    upload_2020-1-28_16-55-37.png
    I don't think that some particles can hide this because the axe is on the other side of tree!