I'm making a 2D game prototype in Unity and I want to have the player attack be a sword that hovers around the player with its direction being fixed to the direction you indicate with the arrow keys. So if the sword is in the upward position and you press the left button, you can see the arc it takes and an enemy in the way of that arc takes damage.
The game is not a platformer. It's on a flat surface.
edit: I want to know how to have the sword hover in a radius to the player and point the direction i press the arrow keys. I want to get the action of moving the sword to these positions to do damage to an enemy. But if the sword is in a upwards position and the player presses down, I want it to do an action that attacks the whole radius around the player, on a cooldown.
How would I go about programming this? I'm new to programming so I would greatly appreciate some helpful pointers.
Edit2:
So this is how far I have gotten: public class SwordSwing : MonoBehaviour {
public Transform swordHolder;
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
swordHolder.transform.Rotate(0, 0, -90);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
swordHolder.transform.Rotate(0, 0, 90);
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
swordHolder.transform.Rotate(0, 0, 90);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
swordHolder.transform.Rotate(0, 0, -90);
}
}
I don't know if this is right, because when I use the arrow keys it just turns the sword 90 degrees and not to the specified position I want it to move to.
So Candid Moon said that I have to use Mathf.Lerp() and StartCoroutine() but I have no idea how to use them. I'm not really sure how to animate this either, I have done some animations in Unity but have a hard time implementing them into anything.
The hierarchy is now:
Player > SwordHolder > Sword
Now I also have this script:
if (GetComponent<Rigidbody2D>().velocity.x > 0)
transform.localScale = new Vector3(1f, 1f, 1f);
else if (GetComponent<Rigidbody2D>().velocity.x < 0)
transform.localScale = new Vector3(-1f,1f,1f);
That flips the player sprite when i move left. This also flips the sword, I don't really want this. How do I stop it?
Let me know if there is anything I missed, I'm trying to learn.