Instantiate an projectile in the direction player is moving

Hi all, my game is a 3d game but the player is locked to moving along 8 axis (ie up, down, left right and diagonals).

In order for me to determine the direction the player is moving in (for the animation scripts) I am using the following code:

    void FixedUpdate()                                          //Fixed update function
    {

        lastInputX = Input.GetAxis("Horizontal");
        lastInputZ = Input.GetAxis("Vertical");

        if (lastInputX != 0 || lastInputZ != 0)
        {
            anim.SetBool("walking", true);
            if (lastInputX > 0)
            {
                anim.SetFloat("lastMoveX", 1f);
            }
            else if (lastInputX < 0)
            {
                anim.SetFloat("lastMoveX", -1f);
            }
            else
            {
                anim.SetFloat("lastMoveX", 0f);
            }
            if (lastInputZ > 0)
            {
                anim.SetFloat("lastMoveZ", 1f);
            }
            else if (lastInputZ < 0)
            {
                anim.SetFloat("lastMoveZ", -1f);
            }
            else
            {
                anim.SetFloat("lastMoveZ", 0f);
            }
        }
        else
            anim.SetBool("walking", false);
    }

So basically the lastMove X and Y will range either -1, 0, or 1 which enables me to identify the direction that the player is moving in.

I also use this to determine the direction of melee attack, like this for example:

            if (Input.GetKeyDown(playerControls.playerOneControlAttack) && (animPlayerScript.lastInputX > 0f))
            {
                StartCoroutine(playerAttackRight());
                playerAttackRight();

What I cannot figure out though, is how can I use this method to instantiate a prefab fireball for example, and fire it in the direction that the player was moving when they press range attack.

I just cant for the life of me figure out how to interpret what I use above into a Vector3, which I assume I will need to do to be able to instantiate this.

Any help would be greatly appreciated :slight_smile:

As your game has 8 locked directions I think it is no use to do vector math to find the direction.

Just keep track of where the player is actually facing, you can use an Enum for that

public enum PlayerFacing
{
TOP,
DOWN,
LEFT,
RIGHT,
[...]
}

public PlayerFacing facing;

[...]
 anim.SetBool("walking", true);
 if (lastInputX > 0)
 {
       anim.SetFloat("lastMoveX", 1f);
       facing = PLayerFacing.RIGHT;
 }
 else if (lastInputX < 0)
 {
       anim.SetFloat("lastMoveX", -1f);
       facing = PLayerFacing.LEFT;
 }
[...]

public struct Forces
{
	public static Vector3 forceLeft = new Vector3(-1,0,0);
	public static Vector3 forceRight = new Vector3(1,0,0);
	public static Vector3 forceTop = new Vector3(0,1,0);
	public static Vector3 forceDown = new Vector3(0,-1,0);
}



public void ShootMagicMissile(GameObject gamePrefab, float speed)
{
	switch(facing)
	{
		case PlayerFacing.RIGHT:
		{
			GameObject g = Instantiate(gamePrefab) as GameObject;
			g.rigidbody.AddForce(Forces.forceRight * speed);
		}
	}
}