How to keep sprites position after Unity animation? - animation

I just recently began using Unity3D and this is my first game, so if I'm not doing things like I'm supposed to, please tell me.
There is a 2D character that I want to move one step forward using the Animation mechanism. This movement can be splitted in two parts :
Move forward the back leg, don't move the upper body ;
Use this leg to push and make the step, moving the upper body.
My problème is that I can't find a way to make this step permanent. As soon as it start looping on the "Idle" animation, then, it come back to the original position (before the step).
I don't know how to let my character stay in the position it was just between the "Step Forward" animation and the "Idle" animation.
I tried to add "Rigidbody 2D", "Box Collider 2D" and "Physics2D Material" on both feet and on the floor, but it somewhat breaks the IK script ("Simple CCD", from the youtube video "Unite 2014 - 2D Best Practices In Unity"). And I'm sure there is a much simpler way.
EDIT: My question was not clear enough. The idle animation is the default animation, where the character is doing nothing in particular. It's just moving a little while it's staying in position. When it runs "Step Forward", all the sprites are moved (only when the back leg is pushing). If all animations are stopped right now, then the character is indeed in a new position, closer to it's opponent. I can check that by removing the transition from "Step Forward" to "Idle". But from here, I want it to loop on the Idle animation. When the transition from "Step forward" to "Idle" is done, the character is NOT running Idle from it's new position, but from it's old position, before the step, as if it was pulled from backward. I want to run Idle from the new position.
Here is a longer gif when we can see more of the Idle animation :
And my Hierarchy panel looks like :
Main Camera
Karateka (the top GameObject container, with the Animator component)
Torso (the sprites)
Head
Right arm
Right leg
Right lower leg
Right foot
...
IK (the Inverse Kinematics targets)
Left fist target
Left foot target
...
Any suggestion is welcome.

If I get it right, you have the StepForward animation running and it gets cut in the middle back to idle. There, the StepForward animation triggers again but from the start and you wish to restart from where you left off:
You can record the normalized value of your StepForward animation and use it next time:
float normValue = this.animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
then you can use that value:
this.animator.Play("StepForward", 0, normValue);
Other possibility, you are not after that but simply you want to pause the animation without getting to Idle.
Use a parameter. You can add a float parameter (call it direction) in the Animator window (top left). Then select a state in the animator, it will open the animation setting in the inspector. Under speed, open the drop down where your new parameter should show up. And tick it.
Now, your direction float will affect the speed of your animation as such:
this.animator.SetFloat("direction", value);
if you give 0, you pause. Give back 1 to move forward. You can also lerp the value so it is not abrupt. Your way.

Related

Eject Blocking Object if Overlapping: UE4

My 2d platformer has a "time travel" mechanic.
You place down a hologram at a location (actor with no collision and some pretty effects).
While you hold down the "Rewind" button, your character's collision and gravity are turned off, and he slowly moves towards the hologram.
When you release the rewind button, it turns his collision back on.
My tilemap is set to "block" the pawn (so he can walk on it, etc).
If you release the button while he's overlapping the terrain, and it turns his collision back on, he gets stuck.
I'm wondering what to do. Unity automatically "ejected" an object that was overlapping something it shouldn't be.
Since the terrain is set up to block the pawn, I can't fire an overlap event. "On Component Hit" will fire just from him standing or rubbing against the terrain, so that won't do either.
How should I detect that the player is popping up inside the terrain? I can decide what to do later (eject them, kill them, prevent them from appearing, etc). But for right now I just need to differentiate from them touching the tilemap and being inside it.
Any ideas?
So what I wound up doing was duplicating the capsule collider on my player. The new capsule (called 'Terrain Overlap') ignores all collisions, except it overlaps the tilemap.
The component has a begin and end overlap method that sets a boolean on the character.
When the player lets go of the Rewind button, if they're inside the ground (boolean check), I can fire the death method.

Why is the animation playing wierd when I check the root motion check box?

So basically I want to make a prefab (a cat) climb a wall, I wanna do it with basic root motion animation. Since I am new with unity and is my first time doing this I decided that I need to do a little bit of testing without any complicated animations, that being said, I made a simple animation, just moving the cat prefab, in the animation clip the cat is moving one meter on the +z axis.
When I play the animation without the "Root Motion" check box checked it works just fine, but when I play it with root motion active (by the way, I checked the root motion check box and I clicked on "Generate Root Motion Curves") it plays in a different way. Instead of moving on the +z axis is moving on the -Z with a shorter distance and a shorter time as well and on the +y axis it moves a much shorter distance (I forgot to mention that I moved the prefab on the y axis as well, to simulate a very basic "jump" so to speak). I am using an animator to trigger the animation right when the game starts and the animation is set to loop.
Why does it do that and how can I fix it so the animation is played with root motion exactly like it plays without the "Root Motion" check box checked? What am I doing wrong?
The problem isnt with your animation. The issue is that Root Motion uses the animation to calculate the movement.
If your script is telling the cat to move, and root motion is telling the cat to move, there will be a conflict. When using root motion, comment out the portion of your script that is telling the player to move one meter.
This should allow unity to do its calculation and move the prefab accordingly. For an in depth explanation, see https://docs.unity3d.com/Manual/RootMotion.html

Unity2D - Player relative animation positioning

I'm making a 2D platformer and I've come across a really annoying problem where if I move my player character GameObject to another location on the scene, my player becomes stuck and the game spazzes out, jumping from the players original location and the position I moved it to.
My player character is made up of many parts, each a separate GameObject. I know the problem is definitely in my animation, because if I disable the animator component, the problem goes away, just I don't have my animations anymore. I believe the problem may be in the player character's Idle Animation and it's position property. There is no script attached where his starting location is hard coded.
How can I make the child Gameobjects move relative to the parent Player GameObject? I cannot move the player in the scene from its original location without the game glitching up.
Here are some screenshots
[Player and it's parts in Hierarchy]http://i67.tinypic.com/bdlc1j.png
[Idle Animation]http://i64.tinypic.com/2gtp99x.png
[Player's original Location, he works if starting here] http://i66.tinypic.com/261jb6c.png
[Player is Moved, game bugs out] http://i67.tinypic.com/292a2c3.png
Try to disable "Apply Root Motion" flag in the Animator component.
The problem should be related to the fact that the animation changes the position values
My guess is that you have animated the different parts by moving then around in the editor and recording that. This meas that the animation is keeping track of the original position at which you did the animation. Try deleting those parts from the animation.
I couldn't fix the spazzy-jittering problem that occurs when I move the player character to another position on the scene, but I did find a way around it. The problem definitely lied within the Animator component and the gameObject's Rigidbody2d.
Instead of moving the player to another position, I instead made a 'Spawn Point' which the player starts from when first playing the scene, and that was able to be moved freely around. Pretty much:
void start(){
transform.position = spawnPoint.transform.position;
}

Pivot for an animation in Unity3D (4.3)

What I want to do:
When a unit lands after falling I want to give it a slight animation of being "squeezed" to the ground and then going back to normal. (Say a slime blob falls and lands, it would get squeezed a little)
Using Pivot "Top Left" on all my sprites the issue that when I scale the sprite in the animation it doesn't scale to the bottom but to the top. My current temporary workaorund is also translating the sprite downwards to counteract that and make it look slike the sprite is being squeezed to the floor. However, there should be some better solution that works with multiple spritesizes and so on.
One thing would be changing the pivot point for just this animation, or maybe a movement thats percentual since the scaling is too. How would I do these though?

Interrupting an Animation

Picture a Windows Phone.
There is a ball (a circle).
When you tap somewhere the ball moves toward that tap.
When it reaches that spot, it stops.
You tap somewhere else.
The ball moves toward that second spot.
BUT, before it reaches its destination, you tap somewhere else. A third tap.
Now the ball changes directions toward the third tap.
To move toward a tap - this is a simple animation.
To change course mid-stream; how do you do that?
If I simply stop the first animation, there is no easing.
I need to change courses gently, smoothly.
An ideas?
If you want the easing to stop right where the ball is at point 2 and then the easing to start moving to point 3, I would do something like this:
Modify endpoint (second spot) to be 10-20 pixels from the balls current location in the direction of second spot.
When the spot reaches the new second spot, start moving toward spot three.
Here is a diagram to help:

Resources