Move During playing Jump Animation - unity3d

So the case is that I have a 36 frames long jumping animation and the jump itself starts at frame 12, where my object raises along y axis.
What would be the most easiest way to move my object forward while it's in air(during frames 12-36)?
How is jumping with animation generally done in games? I don't think my way is the best one. But I would also like to know how its usually done in my way too. Thanks

I have just read the comments and your question. Why not write this code:
Whenever You want him to jump at whatever condition:
Here I'll show you how to do it onClick of Space i.e, whenever you click space the jump animation plays whatever animation you have now the object moving in y-axis
if(Input.GetKeyDown(Keycode.Space))
{
yourPlayerAnimator.setBool("TheAnimatorCondition",true); //Bool for your animation to play if u have a idle with you.
}
if(yourPlayerAnimator.GetBool("TheAnimatorCondition")== true)
{
transform.Translate(Vector3.forward * Time.deltaTime)
}
All these Happens inside void update

Animation transition is your friend.
Put any condition when jumping to move to another animation. In this case i.e. when pressed left displacement key, move from "jump" to "jump&moveleft" or similar and when unpress return to jump state increasing the frames you spent moving to left.

Related

Unity - How To Make a 2d grid movement system

So, I am trying to make a game that requires the user to move up or down on a vertical axis, it needs to be precise however so I want the character to move on a grid, how would this be possible? if anyone could help it would be well appreciated, thank you :)
#voldemort is right, but I can try to recommend a strategy.
Think of checking for each frame, then in the Update () function, if you have pressed a stast, then:
void Update()
{
if (Input.GetKeyDown(KeyCode._upKey)
transform.Translate (Vector2.up * _size_of_the_grid_box);
else if (Input.GetKeyDown(KeyCode._downKey)
transform.Translate(Vector2.down * _size_of_the_grid_box);
}
This code simply moves the gameObject up a certain value when a button is pressed, and down when another button is pressed.

How to keep the animation at the end without restarting?

not loop disable/enable but to keep the animation at it's end frame.
the animation pointing is starting by default but when the state getting to the end the animation ending and not playing again. if i make it loop it will start over again when ending but i don't want it to start over again but to stay on the last frame.
The pointing animation make the player pointing with the finger on a target :
i want the animation to stay at this frame or the last frame so the hand and finger will point all the time and not starting over again.
but this way the hand when the state finish playing move back down.
I tried to enable loop and then to enable also loop pose but didn't work so i tried to change the root transform rotation , root transform position Y , root transform position x-z to bake into pose but nothing of that worked yet.
A working solution is to set the key frames of the animation to the two frames where you want to keep the animation at when playing it.
In my case it's frames 78-79 : I set the frames 78 as the Start and frame 79 as the End. Loop time and Loop pose both disabled unchecked.

Checking collision only during animations

I have this knight character with a sword and an attacking animation. I want to check if the sword is colliding with an enemy and then decrease the enemy health. I managed to do that but EVERY TIME the sowrd's collider hits the enemy's collider I have the interaction. Obviously I don't want that, but the interaction should happen ONLY when the player is attacking.
I tried different approahces:
Check the attack with a boolean, by doing this in the character script
if (Input.GetButtonDown("Fire1"))
{
GetComponent<Animator>().SetBool("hit1", true);
isAttacking = true;
}
and this in the enemy script
if (other.gameObject.CompareTag("Sword") && knight.isAttacking)
{
Debug.Log("Hit");
currentHealth -= 10;
Debug.Log("Enemy healh: " + currentHealth.ToString());
healthBar.UpdateHealthBar();
}
Check if the animation is running using animator.GetCurrentAnimatorStateInfo(0).IsName("nameOfTheAnimationState") instead of knight.isAttacking
Nothing seems to work so far, I know that I'm missing something stupid and I'm going to facepalm when you guys will tell me so, please, make me facepalm!
Thanks.
In the animation window, open the attack animation of the player which roughly looks like this
Now, try playing with the vertical white bar, moving it shows the animation snapshot of the player at each keyframe. Now, find the keyframe at which the player lifts the sword to hit the enemy. Add an event there by clicking the button that's circled red in the image below
After adding the event, you'll see the function in the inspector that's invoked by the event when the animation reaches that keyframe.
Now, create a function to set the attack point active in your script.
Find the keyframe at which the player drops the sword after hitting the enemy and add an event there. Invoke another function that sets the attack point to false.

Unity - Animation Loops?

I am using an FBX 3D object file as a game object in my project which have further sub assemblies. Now i want to animate a part of the assembly.
I followed this process:
Select the part which I want to animate.
In the animation panel create one animation.
Add property > transform > position
Using pivot, i move that part to the location where I want to animate.
Challenges
Animate in loop. Moreover if I Select animation file, it does not show Wrap mode
( 1. I don't want any loops in my animation, even after deselecting loop-time it does loop. - I dont know why?
It is animating in reverse. suppose if the object needs to go from position x:3 to position x:8 but it goes from position x:8 to position x:3
the above point happens in loop. just like a ping pong)
Animates in reverse.
I take the whole animation as one loop i.e Part comes out of assembly and goes back and repeating for infinite time.
I want to stop the animation till the point it comes out.
Any help will be appreciated

Programming controls for sprite

I am trying to make a snake kind of game on the iPhone. For that I plan to have a simple dot as the sprite which draws a line. The dot is controlled by two buttons on the left and the right of the screen enabling it to turn left or right. Now I really do not have any idea how to make the sprite move automatically forward and how to program the turning. It should not be exactly as in snake where it abruptly turns left or right but it should more be a smooth curve. I hope you're getting my point and I'd appreciate all kinds of thoughts! Thanks a lot!
Somewhat trying to make it like this:
http://www.bnet.im/images/screen_curve.png
There are a lot of ways of doing this, but one simple way would be to store the angle of the snake's trajectory (I've called it theta) and move it a fixed amount in every call to Update.
Assuming that the snake class inherits from CCNode:
-(void)Update:(ccTime)dt {
self.position = ccp(self.position.x + cos(theta)*dt,
self.position.y + sin(theta)*dt);
}
You could then update theta from your event handling logic by increasing it or decreasing it when the user taps to turn left or right.