Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Sprites with "anchor points"

Discussion in '2D' started by Viole, Apr 30, 2020.

  1. Viole

    Viole

    Joined:
    Dec 29, 2015
    Posts:
    38
    Hi,
    How would you approach the following problem:
    I want to have 2d hair with physics sticking to the player head, but each animation frame has head positioned differently. I need to find the position of head for each frame and change hair position accordingly.

    Ideally, I would like to have some points in each of my sprites that I can access from a script.
    Is there a way to do this? Or maybe there's a different approach to the problem?
     
  2. nabrown

    nabrown

    Joined:
    Jun 27, 2019
    Posts:
    27
    Seems tough. You could try to create an array of Vector2 offsets that define the relative location of the head of the character during each frame of animation. You can then add scripts to the animator to keep track of the current offset vector. You can use that to move the "anchor point" of the hair.
     
  3. Viole

    Viole

    Joined:
    Dec 29, 2015
    Posts:
    38
    Thanks, it does seem like the only way. By “add scripts to the animator” you mean adding event calling a function on each frame of animation or some other way?
     
  4. nabrown

    nabrown

    Joined:
    Jun 27, 2019
    Posts:
    27
    Within the animation controller view you can click on the different animation states. Each state has the option to add behaviors (scripts) which trigger when the animation state begins/ends/etc.
     
  5. Viole

    Viole

    Joined:
    Dec 29, 2015
    Posts:
    38
    Thanks a lot!