Search Unity

(2D Top Down) How to Change Player Animation when interacting with an object?

Discussion in '2D' started by justincrewsartist, Aug 19, 2022.

  1. justincrewsartist

    justincrewsartist

    Joined:
    Aug 1, 2020
    Posts:
    1
    Hello! I've spent a whole day googling this and absolutely cannot find anything where anyone has ever spoken of how to do this.

    What I'm trying to do: I have a character that's picking up a flower. I currently have the player able to approach the flower (isinrange), and press an interact button (trigger a script on the flower to currently just destroy it if the player is in range and pressing the button). The part that is baffling me is, how do I make the character bend down and lift back up during the sequence? I've tried a couple different things.
    For reference, I want to be able to do something akin to the animation that the character is doing when picking up the crop in this video:


    I've tried to reference the player's animator from within the collectflower script (the one that destroys the flower) so that when "is collected = true" occurs, "PlayerAnimator.SetTrigger("Collect");" where the name of the parameter in the animator is "Collect". In the animator, my current setup is that from entry, I default to an idle blendtree, and from there transition back and forth to a movement blendtree when speed is greater than zero. For the pickup animation, I've made a separate blend tree for directional pickup animations, and have the transitions to and from idle set to the Collect parameter.

    I don't get any errors when I try to run this, but when I "destroy" the flower, the character absolutely does not transition to the pickup state. What am I doing wrong?

    My flower collect script:

    public class FlowerController : MonoBehaviour
    {
    public bool isCollected;
    public Animator PlayerAnimator; //this is to connect the player's animator

    public void CollectFlower()
    {
    if(!isCollected)
    {
    isCollected = true;
    Debug.Log("Flower is now collected");
    PlayerAnimator.SetTrigger("Collect");
    PlayerAnimator.ResetTrigger("Collect");
    Destroy(gameObject);

    }
    }



    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Animation with specific targets is a lot of extra work. It can be done two primary ways:

    - move the player to a known relative position before starting the animation so that it lines up

    or

    - use inverse kinematics (IK) to adjust the animation to reach the flower

    Obviously both of those ways are a LOT of fiddly integration, but if you want, start googling and do some tutorials until you grasp it.

    The best way is generally to do "magic hands," which is a generic reach or squat or wave hands animation that you do for all things. Otherwise you are looking at potentially hundreds of hours of very fiddly animation work for minimal value to the actual game experience.