Problem animating the position of a GameObject in Unity while moving it - unity3d

I am working on moving a Cube across the screen when I press an arrow and display two different animations depending on if it is moving or idle. I used a very basic translation to do this when I press the right arrow. I have two different animation states that work fine independently. One spins the cube when it is “idle” and the other makes to bounce up down in the “right” state. Both are 1 second animations. The idle animation spins the cube 360 degrees, and the right animation will move the cube up 1 unit then back down to its original position. Everything works well by itself. I included an image for the states for the animation.
Cube Animation States
In order to get the movement working with the animations I made an empty GameObject and made the cube a child if it. When the cube is idle it spins, and when I press right it moves and bounces up and down. The problem is when I release the right arrow the cube snaps back to its original position from the start of the last bounce animation. If I hold right for 10 seconds, and release it will only snap back to the beginning of the most recent iteration of the bouncing animation (not to when I started pressing right).
Does anyone know why this would happen? I tried changing some of the settings for the transitions, but it doesn’t seem like that is the problem.

Can you check that your idle animation modifying its position in any case.

First, you can't move the game object with animation.
you can have the rotate animation in the cube but not the translating part.
Because when the animations switch between the states the object will be moved to its original position.
solution:
-Create a script and attach the script to that game object.
-use translate functions to move the object

Related

Character moving up with animation even with "Bake into pose" checked for Root Transform Position (Y) and no apparent movement in animation

DESCRIPTION OF THE PROBLEM
I have a character with a throwing animation that has a Rigidbody with "Use gravity" checked. At the moment of the throw, in the middle of the animation, the character moves up a from the ground.
OBSERVATIONS ABOUT THE PROBLEM
This upward movement does not seem to be present in the animation itself when viewed in Unity's animation inspector, nor is it present in Mixamo, which is where I got it from.
The character never comes down if I DO NOT check the option below and keeps climbing up each time the animation is played.
If I DO check it, then the character STILL goes up but immediately comes down once the animation is finished.
If I check "Freeze position (Y)" as below:
The problem stops but other animations such as death animations stop working as the character floats after dying.
I don't think it's the collider because the character stays well up off the ground after the animation ends and keeps going up more and more each time, staying far away from the ground.
The problem persists with "Apply root motion" unchecked in the Animator.
WHAT I HAVE TRIED
Disabling all scripts, removing the animation event that was present and fiddling with all of the settings that I mentioned here, to no avail.
QUESTIONS
1) Why could this be happening? How could I find the source of this movement given that I don't see it in Unity's animation inspector/Mixamo?
2) Is there a better fix than creating a StateMachineBehaviour script with OnStateEnter and turning on the Y constraint there and turning it off in OnStateExit?
I had the same problem as you.
In my case, the problem came from the definition of the NavMesh from a Plane. For some unknown reason, Unity created the NavMesh plane with a little elevation. Finally, I changed the plane to a cube and the navmesh succeeded, so the agents move well on zero height, as expected.
After many hours reviewing the animations and the different options, the problem was in the generation of the NavMesh.
Hope this can help you.

Unity Animation - Shifting Position Unexpectedly

I'm learning Unity right now, and I want to animate a sprite of Mario running.
I found a spritesheet and have cut out 3 images.
I select my Mario game object and create a new animation
In the animation window, I put in 3 spites at the times I want to animation the run
When I press play, Mario is running, however he is also shifting position a little bit in the X direction but then resetting as the animation loops.
Why would my Mario object be shifting a little bit as part of the animation cycle? Where would I look to see an attached property or behavior that is causing him to do this? Maybe I moved the object while I had record on? I deleted all the associated animation objects and recreated them but Mario is still moving.
I figured it out. I had a pivot point created on a single frame of the sprites. This would screw it up.

How can I make the last sprite in an animation play for more than 1 frame?

I have a 2D sprite animation of a waving flag, which I'm playing in a loop.
The problem I'm having is that the Unity Animation Editor is making the last sprite of my animation play for only one frame, whereas I have every other sprite playing for several frames. So when the animation loops back to the beginning, the last sprite transition doesn't look smooth because the last sprite is visible for fewer frames than every other sprite.
Is there a way to increase the number of frames that my last sprite plays for in the Animation Editor? Or is there a different way to handle this problem?
Here's my animation. I circled where the problem is.
Click on the diamond above 0:00 and copy. Then go to 0:24 and paste it. First and last sprites look the same but if not that is the reason for this and what I propose must work.
Note: BugFinder's solution works as well:
adding a keyframe at the end without changing anything is not the same as an empty one..... The sprite value remains the last one

How do I have an animation always start from the same position on screen instead of the animation playing from its current position?

I'm working on a menu system but am having trouble with the animations.
I need to have it so that at the start of the animation its position starts somewhere else. So if the panel has moved to the right off the screen, when I hit the button it can come in from the left. Instead, no matter what I've done, the animation will just play from where ever it currently is and I can't move its position.
If you're using the Animator system, it always considers the GameObject local position.
So you could parent the animated GameObject and move it's parent around, while preserving the animation coordinates.

Make an object rotate according to mouse movement. [UNITY C#]

I’m starting a learning project. The idea is that you have an archer character that is static, that has a bow attached to it that shoots arrows to targets of varying difficulty.
Turns out that right at the start I’m stuck. How do I make it so that the bow rotates when the player clicks and holds the mouse anywhere on the screen? So I click+hold and move left/right and the bow rotates left/right to aim the shot. I’d like to also eventually make it portable to phones (so you’d tap+hold etc).
Stack Overflow isnt a code writing service but i will explain what you must do:
Method 1 (Exact Aiming):
For every frame the mouse is down:
Make a Ray from a screen point... hint (use
camera.ScreenPointToRay).
Get a far point along the Ray using ray.GetPoint(distance);.
Bow.Transform.LookAt(newPoint, Vector3.Up);.
Method 2 (Continuous Movement):
Make a variable oldMousePos to store a Vector2 location.
Record your initial screen click position into that variable on a
mouse down event.
Have a function that runs once every frame the mouse stays down.
For the direction of the rotation of the bow, you can use
(newMousePos - oldMousePos).normalized;.
For the speed of rotation for your bow, you can use (newMousePos -
oldMousePos).sqrMagnitude;.