Hi. this is the script i use for my gun to shoot. but it only shoots the same direction no matter what way i am looking. how can i fix that?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AssaultRifleCode : MonoBehaviour
{
public float BulletSpeed;
bool shoot = false;
public GameObject bullet;
public Transform bulletpos;
void Start()
{
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
shoot = true;
}
}
private void FixedUpdate()
{
if (shoot)
{
Shoot();
shoot = false;
}
}
void Shoot()
{
GameObject BulletSpawn = Instantiate(bullet, bulletpos.position, bullet.transform.rotation);
BulletSpawn.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, BulletSpeed);
}
}