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 float radius;
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;
transform.position += Vector3.up * radius;
}
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;
}
}