Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Knskan3 · Sep 04, 2014 at 09:02 AM · animationrelative position

[RESOLVED] How to make animation position start from last position

I want to use the unity animations to move an object from A to B everytime I trigger fire1. but the problem is that next time it will start from the begining.

further explanation : http://forum.unity3d.com/threads/animate-position-from-relative-previous-position-2d.265847/

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Knskan3 · Sep 04, 2014 at 12:32 PM

Problem Solved

Unity Animation relative to last position without looping

This code solves the problem regarding animations based on last relative position. In this example, each time we press Fire1 a box will be animated to move from X = 0 to X = 10 Using the animation will help us to provide richer transitions and smooth movements based on curves without the problem of looping.

The idea is to have the animated object inside an empty parent so the animation of the object will be based into local position.

When the animation is finished we update the object and its parent location to match in the last position.

If you have any doubts please ask.

 #pragma strict
 
 /**
 * Animation in Unity Relative to last position without looping
 * @autor Knskank3
 * http://stackoverflow.com/users/1287772/knskan3
 * 04/09/2014
 **/
 
 /*
     This code solves the problem regarding animations based on last relative ImagePosition
     In this example, each time we press Fire1 a box will be animated to move from X = 0 to X = 10
     Using the animation will help us to provide richer transitions and smooth movements based on curves
     without the problem of looping
 */
 
 
 // This var will determine if the animation is started
 public var animation_started : boolean = false;
 // This var will determine if the animation is finished
 public var animation_finished : boolean = true;
 
 function Update () {
     
     // if user triggers Fire1
     if( Input.GetButtonUp('Fire1')){
 
         // initialize the flags
         animation_started = true;
         animation_finished = false;
 
         // Start the animation
         // this animation moves the box from local X = 0 to X = 10 using a curve to deaccelerate
         animation.Play("boxanim");
     }
 
 }
 
 /* This function is trigger at the end of the animation */
 public function animationFinished() : void {
     animation_finished = true;
 }
 
 /*  
     At the end of the frame if the animation is finished
     we update the position of the parent to the last position of the child
     and set the position of the child to zero inside the parent.
 */
 function LateUpdate () {
     // if the animation is finished and it was started
     if(animation_finished && animation_started) {
         // set the flag
         animation_started = false;
         // update the parent position
         transform.parent.position = transform.position;
         // update the box position to zero inside the parent
         transform.localPosition = Vector3.zero;
     }
 }









Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GluedBrain · Sep 04, 2014 at 01:07 PM 1
Share

Animator should not be attached to the parent object. Parent should not have any animation component attached to it. Whatever you want to animate, it should be within that parent. Also, try re animating the child object if the existing animations still play in the initial position.

P.S. Use the comments section to clarify.

avatar image Knskan3 · Sep 04, 2014 at 05:30 PM 0
Share

Imagine I have I character that attacks. If you press fire1 you step forward and kick. The next time I press fire again I want the character to be placed where it was at the end of the step.

What I want:

idle[ x = 0 ] --> --> press Fire1 [ x = 1] --> idle [x = 1] ( remains in x = 1 not x=0) --> press Fire1 [x = 2]

This way the character is moving forward by animations. not by moving the position by any other way.

avatar image Knskan3 · Sep 04, 2014 at 06:40 PM 0
Share

Resolved, post edited :)

avatar image correia55 · Oct 23, 2015 at 07:29 AM 0
Share

How and where do I trigger the function animationFinished() at the end of the animation? Can you please give an example.

Thank you in advance.

avatar image Knskan3 correia55 · Oct 23, 2015 at 04:28 PM 1
Share

You have to add this call in the last animation frame. Here you have a picture: http://forum.unity3d.com/attachments/animationevent-png.119387

avatar image Magnumstar Knskan3 · Jun 13, 2016 at 08:03 PM 0
Share

Excellent, this is amazing.

avatar image elpuerco63 · Mar 25, 2016 at 02:35 PM 0
Share

I kind of follow this but am failing to get it implemented probably due to the setup of the prefab that is beyond my control to modify which has this layout:

ParentGameObject (has an AnimationController attached) ->GameObject_LOD00 ->GameObject_origin -->Root

When I run the scene the animations do what this thread describes as in reverting to starting position ins$$anonymous$$d of remaining in place

I an empty GameObject and place the above into it, so now the parent does not have the animation controller, but I still get the same unwanted behaviour.

But as the script you supply is attached to the new empty GameObject the events in the animation controller are orphaned and so flag up as not assigned?

avatar image
3

Answer by AndreBR · Jan 14, 2016 at 11:43 AM

I solved by: - Accessing the Animator interface, - clicking on previous animation state, - clicking on "Add Behavior" button and creating the following script:

override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {

 animator.transform.parent.gameObject.transform.position = animator.transform.position;

}

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image deadboltinteractive · Jan 17, 2016 at 06:50 PM 1
Share

You can simplify that statement like this:

  animator.transform.parent.position = animator.transform.position;
avatar image AdmiralThrawn · Aug 13, 2017 at 06:41 PM 0
Share

Best solution!

On a side note: For those who like to utilize "root motion" for animations that manipulate RectTransform values. Nope, that won't work. That feature only works with transform components :( (Well, at least not in Unity 2017.1).

avatar image smatthews1999 · Jan 08, 2019 at 01:26 AM 0
Share

Best answer! I have never understood those behaviors. Thanks!

avatar image troya_carlos · Sep 12, 2020 at 06:05 AM 0
Share

This doesn´t Work for me. :..( Always show me a NullReferenceException.

  • Help

!

avatar image
1

Answer by GluedBrain · Sep 04, 2014 at 11:09 AM

First of all make sure you have applied the Animator component to the child object. If the object that you want to animate is the parent object, then create an empty gameobject and place your object with animator component inside this empty object. Secondly, you got to uncheck the apply root motion checkbox of the animator component.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Okiski · Feb 22, 2018 at 02:58 PM

Create an empty animator object (my empty's name is "New State") and use that script

 Animator anim;
 
 anim = GetComponent<Animator>();


 public void AnimationFinished()
 {
     anim.Play("New State");

     transform.parent.position = transform.position;
     transform.localPosition = Vector3.zero;
 }

and then this function will be in the end of the animation (you can do it from here https://forum.unity.com/attachments/animationevent-png.119387/ ) and that s it.

dont forget this

     anim = GetComponent<Animator>();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

Mouse Location in world relative to the player 2 Answers

Can I make animations snap to a frame? 1 Answer

How to select an animation clip by index number? 7 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges