I have downloaded the asset Bézier Path Creator by Sebastian Lague and want to use it for a 2D game object, an enemy (npc) Spaceship, so that game Object follows the set Path and while moving up and down makes up and down tilting movement animations (the spaceship tilts up and down).
It is easy to make the Spaceship follow the path but how can I animate it when moving down and up the spine?
You need to get the direction of travel then apply that to the rotation of the spaceship.
Vector2 direction = targetPosition - currentPosition;
direction.Normalize();
transform.right = direction;
The above code requires that the sprite be facing to the right. You will need to change this in your image editor.
I usually set everything to face the right with 2D. Why? 0 degrees is right facing in Unity, so rotations don’t need offsets to face the right direction.
I think I might need some more help as I’m rather a bloody beginner and hope I’m not bothering too much with this. I also think I need to be more specific.
I have made a script for my Player spaceship that makes Tilt animations when moving Up or Down and want to do the same for the NPC Spaceships (it’s a side scrolling shoot’em up game). The Player flies from left to right and is turned to the right and the NPCs come from the left and are facing left.
I used the Animator to animate this and have two different Tilt animations when moving up and two different Tilt animations when moving down (depending on how long you move up or down the according animation is displayed). Also there is an animation for the Shooting in every possible position. Please look below:
For the movement animation I used a Float Parameter “VerticalSpeed”. For example for the Moving UP1 animation from the Idle Position the transition is “VerticalSpeed” is “Greater” than “0” (here this one example):
All this works fine and all Animations play as they should for the Player.
Now for the NPC Spaceship I want to do exactly the same but the whole System is of course different since I can’t do this via the “Input.GetAxis” System as for the player and below the lines of code I did to make the animations work for the Player:
For the NPC Spacehip, as I’m a bloody beginner, I used the Bézier Path for the movement direction and the NPC spaceship follows the Path perfectly fine but of course without the Tilt animations that I want similar to the Player animations.
Below you can see the NPC spacehip and the Bézier Path it follows and when moving down I want it to do the Tilt down Animation and hen moving up I want it to do the Tilt up animation just like the Player.
Here is the Code for the NPC Spaceship following the Bézier Path:
Now how can I apply the same Tilt Animations for the Player to the NPC while it’s moving up and down along the Bézier Path?
Please note again I’m a bloody beginner and just started a few months ago as a hobby and really would appreciate if you could tell me how to proceed respectively where to put in the code lines.
It can be done I’m sure. I have a little bit of an idea of how to tilt it, use the direction and if y > 0 then tilt up and if y < 0 then tilt down. Other than that I don’t have a clue and not sure if it would look right.
It now plays the animation by searching the difference from the y position but the problem is with this code it won’t makes it impossible to show the Idle animation as my float parameter “VerticalSpeed” in the Animator can only be greater or less than “0” and “0” equals the “Idle” position/animation.
But it’s already a little success as for up and down splines it works fine.