Object stuck in air, how to fix

Hope this wont be a problem im going to post this question here since my question has been waiting for moderator approval for a few hours now and I really want to see if this can be fixed.

So I have an object that I have it to start in air and the gravity will pull him down until it collides with the ground. The problem comes when I try to animate it with positions changes. I have 3 animation in this object right now, 2 of these animations are simple ones that I control via code, like drag and drop sprites into it without changing position. The Object works fine with these animations,it will drop when I hit play and everything works fine. The problem comes,like I said before, when I animate changing positions, like I put a sprite then put another and change its position, put another sprite and change its position, etc, etc, when its finished it looks like the object is moving from one place to another normally. Now when I hit play the Object gets stuck in its starting point(I have some draw lines and I can see the lines going down then reappearing where they start very fast).In animator i can see a state I called “Idle” playing and the one I made with the changing position not playing but still I have this problem only when I make these kind of animations. The only way to fix this is by deleting the animation and leaving it with animations that not change positions. I would just code the movements/patterns but I feel like these kind of animation can be useful for animations that I don’t want to have that much control over them. I tried to google this problem but its hard to even explain it… at least I know that the problem only comes when I change positions in the animation so I guess that at worst I would have to code. Anyone know why this is happening? Help is greatly appreciated, have a nice day. Also one thing I would like to add is that I am using 2D , don’t think that would matter but yeah…

If your object has a rigidbody, then using positional change animations isn’t the proper way to go, but if you must, then set the rigidbody to “isKinematic” during the animation.

As for being stuck, well firstly, animations don’t work relative to your position, they are absolute world space positions, so if you character dropped, and you played a movement animation that was started from a different position in space, it’s going to go to that absolute position, not one relative to the character’s current position.

Also, I believe that even if the animation finished, if it has no other place to transition to in Mecanim/Animator, then it’s going to continue updating your object with whatever values are at the end of the current animation (not 100% right now, but pretty sure)

What exactly are you trying to achieve? If you can explain that better, we might be able to guide you in a better direction.

Yeah i made a physcis2d.linecast to know when the object is in a specific place in the scene that way it would only play the animation there and not look like its teleporting. Also went to the inspector and tried what you said about checking isKinematic during the animation and it still gives me the same problem. If I set isKinematic for the whole object which gives it to all the animations then it works but gets rid of the gravity effect and that’s something I need on the object.

To answer your question
I remember that in donkey kong country the last boss would jump until it hit one side and then jump to the other side, if I were to implement something like that using unity it would be something like this

if(collideRightside(or hit line in right side)==true){
transform.Translate (-Vector2.right * Speed * Time.deltaTime);
if(isGround==true){
rigidbody2D.AddForce (Vector2.up * jump_Force * Time.deltaTime);
}
}

I saw that in unity i could move my sprite in the scene and it would record it and play it so i thought i could take that animation put it in the animator and make a parameter to play it when i want/needed to. Obiously that isnt working for me lol.

Basically I just want to create patterns for my object and also scripted moments.

Also thanks for taking off your time to look into this and give help, its appreciated.

Yes, isKinematic gets rid of the gravity and other forces, but thus also allows the object to be animated properly. Could you not simply set isKinematic back to true at the end of the animation? It’s an animation keyable parameter.

Or simply just script the boss so that when he hits the wall, he turns around, plays a jump sprite animation (but not movement) and has his rigidbody2D.velocity set to the direction/arc you want him to jump in, for example,

rigidbody2D.velocity = new Vector2(40,30);

You’d just tweak this value to get the height and distance you want out of the jump, just like you’d tweak and animation for such a specific purpose as you’re trying to do. And let the gravity/physics do the rest of the work in a believable way. (Though if you have players or objects that can physically collide with him, this could be a bad choice as they could collide and push him out of the jumping arc… Depends if you’re doing body collisions are just trigger = hurt collision.)

I finally found something that works for me. I made a object with a child , this child will hold my animations and everytime I need them I will just transform.position the object I want to play the animation to these childs position(and start the animation of these child when the object is in position too).

Im preety satisfied with this since it will make things a lot more easier for me. Anyone that wanna try this should be aware that this can cause some problems if not done right, like teleporting your object from one place to were the animation start, but if used right it should work just fine.