How to move an animated AI character in Unity3d - unity3d

Current Situation
I have a character and a "walking" animation.The animation is used in an animation controller. All assigned and done with the Inspector, no scripts involved. "Loop" is active.
When I hit the "play" button, the character's position moves according to the animation.
Problem
Once the animation reaches the end, the character gets relocated back to the origin point.
Question
How do I achieve that the character continues walking forward?
I read that I should activate "apply root motion" which I did, but that doesn't change anything, the character's position still gets moved back to the origin after the animation ends.

do all your animations in place and move your object via code , change player position with code and let your in place run animation play.

I really dont think that this can or should be done without any scripts.
Even currently playing animation should depend on parameters of the animator, as well as the movement itself.
And you won`t have to write a separate script for every state.
One script can get the user input, assign animator parameters (e.g. if the player had pressed LeftCtrl it sets 'Crouch' parameter to true and animator triggers related animator state/transition) and update character position depending on the speed and direction values.
In order to save some time finding appropriate value of movement speed to match your animation, you can add an animation speed multiplier to every state in the animator and make it a parameter. In that case one variable with speed value will modify actual movement speed and animation framerate.

Related

Animation changes transform when entering game mode

In my Unity 3D project I've added an animated body as a child to the Player object. The player moves as is should, with the right animations displaying for each key. The problem however is that the transform of the child changes when I enter game mode, meaning the character is walking above the ground. In scene mode it's relative to it's parent, it's grounded, but when I switch to game mode the character suddenly changes it's y-axis transform. I also checked the inspector for this object, it's y-axis changes.
I tried disabling the movement script, to check if this could have anything to do with it, but nothing changes. The transform still changes. I'm new to this problem, and every thread I've found on the topic hasn't helped much. Does anyone know what might cause this?
The hierarchy. mixamorig:Hips is the one changing it's transform. The animations and animation skin I'm using is from Mixamo.

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.

how can I implement a Unity fps sprinting animation?

I'm currently working on an FPS game and I have these hand models as well as weapons. But I want it so that if I press the W key (move forward) it plays the sprinting animation with the arms. I initially thought of coding it like, if(Input.getkeydown(keycode.w))animation.play or something like that. But I'm using the character controller which doesn't use that. It uses the Input.getaxisraw. How should I implement this with the character controller?
Thank you a ton in advance
You will need to implement a basic animator controller. You can define all your states (Idle, Crouch, Jump, Sprint, Aim) and define the transitions between them. You may then add parameters like floats, triggers and booleans as conditions to transition between these states.
In the above example, if you want to move to a crouch state, you would use
GetComponent<Animator>().SetBool("Crouch", true);
Or for another example, if you want to move from Run to Walk. The transition (The blue lines indicating the movement from one state to another) will have a condition to move from walk to run and back based on the float parameter "Speed". if Speed>x, move from walk to run. else move from run to walk. In this case you would set
GetComponent<Animator>().SetFloat("Speed", floatValueHere)
and the controller will take care of the animation transitions.
This is just a pointing to the right direction. you will need to watch a couple of videos on animator controllers Check Unity's simple 8 minute explanation here Hope this helps!

Unity3d Animation - How to make the player to stay at the target position after the animation completes

Created a simple animation to move a cube from (0,0,0) to (5,0,0).
When the animation stops, cube is going back to (0,0,0). How to make sure that the cube stays at (5,0,0) even after animation completes
You could also checkout the AnimatorState WriteDefault value.
Whether or not the AnimatorStates writes back the default values for properties that are not animated by its Motion.
By default it is enabled so if the State is left the Animator resets to the default values of your inanimated objects.
If you disable this value than the last state stays persistent although the state is left or the Animator disabled.
Got it working using simple Animator state machine with idle, moveLeft and moveRight states and appropriate triggers!!!

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

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