Shooting in direction of the character movement

Hello,

I am fairly new to Unity, I have a question regarding shooting bullets in the direction that the sprite is moving. I have looked at the following 2 tutorials:

https://www.youtube.com/watch?v=1mw1ufZq1N4&t=111s

while both come close to a solution, I can’t seem to figure out the best way to achieve the desired effect. Both of the tutorial above use either a cursor to aim and shoot or directional keys to rotate the sprite.

I need the sprite to be able to move in 8 directions with animations using the arrows keys and using the Fire1 button to shoot a bullet. I do not know how to get the bullet to travel in the direction the sprite is moving at the time it is fired.

Any help or directions is much appreciated.

Thanks

It depends on how you are moving the bullets. Using Translate() or AddForce(transform.up) the bullet can be instantiated to the same rotation as the player. Using AddForce(Vector2) the bullet needs the global values depending on which way the player is facing. e.g.

if(player.GetComponent<SpriteRenderer>().sprite.texture == facingRightTexture)
    {
    bullet.GetComponent<bulletScript>().addForceVector2Variable = Vector2.right * speed;
    }

and on the bullet

GetComponent<Rigidbody2D>().AddForce(addForceVector2Variable, ForceMode2D.Impulse);

Hello @Magso

Based on your feedback, I used the animator to get the direction of the character. using that direction I was able to apply the correct rotation and offset to the bullet object to get the desired effect. Below is the code that I am using to do this.

        private void Update()
            {
                cooldownTimer -= Time.deltaTime;
        
                if (Input.GetButton("Fire1") && cooldownTimer <= 0)
                {
                    // Debug.Log("Pew!");
                    // Shoot the gun
                    cooldownTimer = fireDelay;
                    int shotRotDeg = 180;
        
                    int aniHor = (int)Math.Round(animator.GetFloat("Horizontal"));
                    int aniVer = (int)Math.Round(animator.GetFloat("Vertical"));
                    float offsetX = -0.14f;
                    float offsetY = -0.4f;
        
                    // Down		0, -1
                    if (aniHor == 0 && aniVer == -1)
                    {
                        shotRotDeg = 180;
                        offsetX = -0.14f;
                        offsetY = -0.4f;
                    }
                    // DownRight	1, -1
                    else if (aniHor == 1 && aniVer == -1){
                        shotRotDeg = 225;
                        offsetX = 0.55f;
                        offsetY = -0.1f;
                    }
                    // Right		1, 0
                    else if (aniHor == 1 && aniVer == 0)
                    {
                        shotRotDeg = 270;
                        offsetX = 0.55f;
                        offsetY = 0.12f;
                    }
                    // UpRight		1, 1
                    else if (aniHor == 1 && aniVer == 1)
                    {
                        shotRotDeg = 315;
                        offsetX = 0.4f;
                        offsetY = 0.5f;
                    }
                    // Up		0, 1
                    else if (aniHor == 0 && aniVer == 1)
                    {
                        shotRotDeg = 0;
                        offsetX = 0.25f;
                        offsetY = 0.5f;
                    }
                    // UpLeft		-1, 1
                    else if (aniHor == -1 && aniVer == 1)
                    {
                        shotRotDeg = 45;
                        offsetX = -0.4f;
                        offsetY = 0.5f;
                    }
                    // Left		-1, 0
                    else if (aniHor == -1 && aniVer == 0)
                    {
                        shotRotDeg = 90;
                        offsetX = -0.55f;
                        offsetY = 0.12f;
                    }
                    // DownLeft	-1, -1
                    else if(aniHor == -1 && aniVer == -1)
                    {
                        shotRotDeg = 135;
                        offsetX = -0.55f;
                        offsetY = -0.1f;
                    }
        
                    // Vector2 shootingDirection = gun.transform.localPosition;
                    Vector3 offset = new Vector3(offsetX, offsetY, 0);
                    // shootingDirection.Normalize();
        
                    shootingDirectionX = animator.GetFloat("Horizontal"); //shootingDirection.x;
                    shootingDirectionY = animator.GetFloat("Vertical"); // shootingDirection.y;
        
                    GameObject bullet = Instantiate(bulletPrefab, player.transform.position + offset, Quaternion.identity);
                    bullet.transform.Rotate(0, 0, shotRotDeg); // Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
        
                }

I am not sure if this is the best way to go about this, but for now it is working and I can start looking in to other parts of the game. Thank you so much for your help and guidance, it really helped me out.