How do i shoot in the direction im facing?

Hi guys I’ve tried everything that I could think of but sadly im a huge noob. Can anyone help figure out why I cant shoot to the left. This is my shoot script attached to the shoot point.

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.K))
    {
        Rigidbody2D instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody2D;
        instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(20, 0, speed));

        projectile.velocity = new Vector2(velX, velY);
    }
}

}

Here is my Controller script.

{
    rb = GetComponent<Rigidbody2D>();
    spriteRenderer = GetComponent<SpriteRenderer>();
    animator = GetComponent<Animator>();
}

void Update()

{
    Vector2 move = Vector2.zero;

    float horizontal = Input.GetAxis("Horizontal"); // a or left = -1 d or right = 1

    rb.velocity = new Vector2(horizontal * Speed, rb.velocity.y);

    animator.SetFloat("Speed", Mathf.Abs(horizontal));

    if (Input.GetButtonDown("Jump") && grounded)
    {
        velocity.y = jumpTakeOffSpeed;
    }
    else if (Input.GetButtonUp("Jump"))
    {
        if (velocity.y > 0)
        {
            velocity.y = velocity.y * 0.5f;
        }
    }

    animator.SetBool("grounded", grounded);
    animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);

    targetVelocity = move * maxSpeed;
}

}

This is my flip script as well:

// variable to hold a reference to our SpriteRenderer component
private SpriteRenderer mySpriteRenderer;

// This function is called just one time by Unity the moment the component loads
private void Awake()
{
    // get a reference to the SpriteRenderer component on this gameObject
    mySpriteRenderer = GetComponent<SpriteRenderer>();
}

// This function is called by Unity every frame the component is enabled
private void Update()
{
    if (Input.GetKeyDown(KeyCode.D))
    {
        // flip the sprite
        mySpriteRenderer.flipX = false;
    }

    // if the A key was pressed this frame
    if (Input.GetKeyDown(KeyCode.A))
    {
        // if the variable isn't empty (we have a reference to our SpriteRenderer
        if (mySpriteRenderer != null)
        {
            // flip the sprite
            mySpriteRenderer.flipX = true;
        }
    }
}

}

So when you do a flip on a spriteRenderer, it doesn’t change the objects orientation. Thus when yo do something like, (transform.TransformDirection(new Vector3(20, 0, speed)); ) this will still shoot towards the right.


You could manually keep track of that flip in an int flipState, where 1 is to the right, -1 is to the left. Change the int when you flip the renderer. Then just multiply this int value to the transform.TransformDirection(new Vector3(20, 0, speed)) * flipState; Where a positive transform is right, negative is left.


Hopefully that makes sense.

@ReubenClare123
Quaternion.identity :slight_smile:
if you want to shoot a bullet in front of you character or gun you do simply :
Instantiate(bulletPrefab, transform.position, Quaternion.identity);
this will set the bullet rotation like you character’s. then just give your bullet game Object a scrip to move forward.