# Firing bullet in direction of mouse?

My goal is to fire a bullet from a fire point set inside my weapon, but all it does it shoot up.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAim : MonoBehaviour
{
public Transform weapon;
public GameObject player;
public GameObject projectilePrefab;
public GameObject firePoint;
public float projectileSpeed = 40;
public SpriteRenderer sRend;

private Transform pivot;

void Start()
{
pivot = weapon.transform;
transform.parent = pivot;
}

void Update()
{
Vector3 weaponVector = Camera.main.WorldToScreenPoint(weapon.position);
weaponVector = Input.mousePosition - weaponVector;
float angle = Mathf.Atan2(weaponVector.y, weaponVector.x) * Mathf.Rad2Deg;

pivot.position = weapon.position;
pivot.rotation = Quaternion.Euler(0f, 0f, angle);

if (Input.GetMouseButtonDown(0))
{
Shoot();
}

if (Mathf.Abs(angle) > 90)
{
sRend.flipY = true;
} else
{
sRend.flipY = false;
}
}

void Shoot()
{
GameObject projGO = Instantiate<GameObject>(projectilePrefab, pivot.position, pivot.rotation);
projGO.transform.position = firePoint.transform.position;
Rigidbody2D rigidB = projGO.GetComponent<Rigidbody2D>();
rigidB.velocity = Vector3.up * projectileSpeed;
}
}
``````

This is why it only goes up:

``````rigidB.velocity = Vector3.up * projectileSpeed;
``````

You could try doing this if “pivot” faces your target:

``````rigidB.velocity = pivot.forward * projectileSpeed;
``````

Thanks! For some reason, though, the bullet doesn’t shoot out? It seems to appear for a second on the firepoint, then spawn on the ground in the player’s start position.

Maybe try pivot.up * projectileSpeed instead of Vector3.up …I was just thinking you’re rotating the weapon in Z, so forward would be into the distance / wouldn’t work in 2D