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

Question Update position in Animation?

Discussion in 'Scripting' started by bisewski, Oct 4, 2023.

  1. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    120
    Hi!

    I have an animation that I did using Unity Animation. One box move to a target position. I start the animation by clicking in a button.
    To back to the original position I just click again the button and use the Animation in reverse mode with time in -1.

    But the problem is that I need to change the position of this box. So the animation move the box to the target point, I want to move this box to anywhere and when I click in the button, back to the original position from the new position.

    Now, when I click in button, the box jump to last position in animation and back to the original position...

    Is possible do it with Animation? Like update the position?

    The animation is more complex, this is just a example.

    Do this sample by code is easy, but the problem is that like I said, is a complex animation...And do all by code will be hard.

    Thank you for attention.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    If you do the complex animation in place you can tween it around anywhere you like.

    This includes positioning and rotating it however you like.

    Most games use some form of hybrid animation along with tweening.

    That's all walking is. You can walk anywhere you want in a game, but you use the same animation.

    If it is fire and forget, just use a tweener like LeanTween or DOTween. No sense rewriting.
     
    bisewski likes this.
  3. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    120

    The problem is that my code is ugly. I dont know how to do it withot use many ifs...
    If I do my way animating everything from a script, lerping vector3 or floats will be more flexible to me. I could start, pause and reusme or stop the animation much more easy then Animation Unity.

    The problem is how to create the sequence. So the object1 start lerping, after some few time object2 also start. When second object stop we can start other two object lerping the transform...

    How to do this timeline? I can do using many ifs but the code is ugly...
     
  4. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    120
    And now to help, I am in other throuble. I need to advance and back in an animation.
    So I put some Events in some frames. And call a function that set the Animator speed to 0. So I stop. And I can set the speed to 1 and advance until next event frame.

    But now to reverse, to set the speed to -1 doest work.
    So we cant back to the last event frame.

    What I need is so simple and I am not understanding why animation is so hard in Unity..

    I have one animation for example 1 minute. And I need to stop in some points...Click in a button and go from that stop to the next or back to the last...

    Is so simple..
     
  5. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    303
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Unity's animation system is incredibly simple and easy to use.

    Source: I use it all the time. Our entire team of artists use it all the time. And they do CRAZY stuff.

    Therefore I must conclude one of the following:

    - you haven't taken the time to understand the animation system via tutorials, in isolation, away from your project

    - or what you are doing is actually far more complicated than you believe it to be (doesn't sound like it to me!)

    - or you have a bug. In that case, go fix it!

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
    bisewski likes this.
  7. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    120
    Sorry. I didnt update.

    I did it using a SetFloat where we can change a parameter that will multiply the current velocity.. So it worked, more or less.

    The problem is that Events setted in current clip doesnt stop in exactly frame that they are setted. Because of course the animation is running untill the Events call the function that will stop the animation. So when stopped the position now inst really over Events frame, but a little more few milisseconds.

    So when we back, the first Event inst the previous Event, but actually is the same. Because now we are backing only this few milliseconds...This happens only when in reverse mode. So I did an ugly code with a bool to test when we are in reverse mode and simple jumping the Event frame. Not really jump, the function will be called but after test, will not set the speed to zero.

    Sorry, I only thought that is strange so many lines of code just to manipulate a clip.

    This lines is to get the frame position:
    MyAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.length * (MyAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime % 1) * MyAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.frameRate;

    Maybe "MyAnimator.clip("my clip").position" will be more intuitive?