Reputation: 11319
I added 3 bullets without Rigidbody to the 3 holes in the gun manually so I can get the start positions from where the bullets will come out. This 3 bullets are also children of the gun barrel that spin nonstop.
I created a bull prefab, the prefab have a Rigidbody. The script is attached to the gun barrel.
This is a screenshot showing the 3 bullets in the holes just to be able to get from them the start positions. It's showing the Drone with the gun in the hierarchy and the script in the inspector :
This screenshot is showing what happens when I click the mouse left button fast and shoot some bullets. It looks like more a popcorn machine then bullets :
The bullets are coming out from the 3 holes in the gun barrel but they are falling too fast and shot very short distance. What I want to do is that the bullets will be shot forward according to the barrel facing position and that the bullets will hit objects walls and other stuff and later to add damage to the object that got hit maybe using Raycast. but now I can't make the bullets to be shot as a bullet should be.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ShootBullets : MonoBehaviour
{
public GameObject bulletPrefab;
public float bulletSpeed = 100f;
public bool automaticFire = false;
private List<GameObject> startPositions;
// Start is called before the first frame update
void Start()
{
startPositions = GameObject.FindGameObjectsWithTag("Bullet").ToList();
}
// Update is called once per frame
void Update()
{
if (automaticFire == false)
{
if (Input.GetMouseButtonDown(0))
{
for (int i = 0; i < startPositions.Count; i++)
{
GameObject bullet = Instantiate(bulletPrefab, startPositions[i].transform.position, Quaternion.identity) as GameObject;
Rigidbody bulletRB = bullet.GetComponent<Rigidbody>();
bulletRB.AddForce(Vector3.forward * bulletSpeed);
bulletRB.velocity = bullet.transform.forward * 40;
Destroy(bullet, 0.5f);
}
}
}
else
{
for (int i = 0; i < startPositions.Count; i++)
{
GameObject bullet = Instantiate(bulletPrefab, startPositions[i].transform.position, Quaternion.identity) as GameObject;
Rigidbody bulletRB = bullet.GetComponent<Rigidbody>();
bulletRB.AddForce(Vector3.forward * bulletSpeed);
bulletRB.velocity = bullet.transform.forward * 40;
Destroy(bullet, 0.5f);
}
}
}
}
I tried first to use only AddForce then only velocity then both but it didn't much change the results.
Upvotes: 0
Views: 705
Reputation: 467
set "drag" parameter to zero on bullet's rigidbody. Drag continiously slows rigidbody down.
On this line:
bulletRB.AddForce(Vector3.forward * bulletSpeed);
your bullet will fly in Z-axis direction, no matter where your barrel aimed.
You set the right vector here:
bulletRB.velocity = bullet.transform.forward * 40;
ok, almost right: this vector points in direction of bullet's local Z axis, but when instantiating, you set rotation as Quaternion.identity, which means zero rotation. So, none of these not allows you to control bullet`s direction.
What you need, is to use barrel's forwad vector as bullet force. And its better to use AddForce, with proper Impulse mode, as mentioned in a comments. Like this:
bulletRB.AddForce(barrelTransform.forward * bulletSpeed, ForceMode.Impulse);
Upvotes: 1