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. Dismiss Notice

[Solved] Scaling animation position by object size

Discussion in 'Animation' started by Boogafreak, Jun 24, 2018.

  1. Boogafreak

    Boogafreak

    Joined:
    Oct 2, 2017
    Posts:
    42
    Hi,

    I have a simple square object (tile in a game), which I want to move to the right.
    The issue is that I want the tile to move right by exactly its' size, but I can place only numbers and not variables in the animation -> Tile : position.x .
    I wish to place the tile size there, which is a variable.. The tile size is dependent on many parameters, such as the 2 canvases above it, and some calculation on the inner canvas, but I can access the final size Tile:Width.

    So.. how do I place Tile:Width in the animation -> Tile : position.x ?

    Note : I found https://answers.unity.com/questions/414940/can-i-scale-an-animation-with-a-model.html , and tried to do that, but I don't ever see my game object in the second drop-down menu (in step 4).
    It only writes "Samples".

    Please help.
    Booga.
     
  2. Boogafreak

    Boogafreak

    Joined:
    Oct 2, 2017
    Posts:
    42
    Please help.. any way to scale animation keyframes with the object animated??
    I dug up that I can create the keyframes and then a whole animation from scratch in code during runtime, but that seems excessive for a simple animation on one axis that needs a variable instead of a value :(
     
  3. Boogafreak

    Boogafreak

    Joined:
    Oct 2, 2017
    Posts:
    42
    I've progressed in other game mechanics, but I'm still stuck on this. [nearly crying]
    I know not many people are here, but please help if you can.

    I'm simply instantiating a tile, then resizing it. but the animator keeps moving 90 pixels - not regarding my resize.

    My current options for dealing with this -
    1. Create my own animation via a coroutine that loops wait and set local position.. sadly, this may be my best solution.
    2. Try placing the tile in a parent object and set the animator to the parent object... I understood that somehow if I resize the parent object, the child (tile) animation will be resized - is this true?
    3. Check if scaling the animator parameter is possible (?), and if so scale it, and at end of animation set position so it would be pixel perfect.

    I theoretically also have an option to turn to "Animation" instead of "Animator". But Animation is legacy, and I won't start dealing with a legacy component.

    There must be a simple solution for this.. I know there is. I just don't know what it is. :(
    Booga. going to relax.
     
  4. Boogafreak

    Boogafreak

    Joined:
    Oct 2, 2017
    Posts:
    42
    Finally found a solution (I am not arguing it's the best one) :
    Change the scale of the object (the tile) instead of its' exact size.
    Changing the scale of the object keeps the animation working on the same size with the new scale..

    My tile's RectTransform is a variable called rectt, so when I want to resize it to tile_size I just replaced
    rectt.sizeDelta = new Vector2 (tile_size, tile_size);
    with
    rectt.localScale = new Vector3 ( (float)tile_size/original_tile_size , (float)tile_size/original_tile_size , 1);

    And the animation is working with every new tile_size I throw at it.

    The only issue is that the animation takes the tile not to an exact position (instead of x=2 it ends in x=1.99964),
    but this can easily be fixed with a set local position after the animation ends.