What are the correct methods of bullet management for 3d top scrolling shooter like “Raiden”, “R-Type”, “Gunbird” ? I want to make some lightweight code for bullets, without rigidbody, so the bullet just uses “bullet.transform.position = transform.forward*speed;” it moves forwards until it hits a collider, then the bullet can use “on collision enter” to play a sound. I dont think i need rigidbody, and those things? Is it wrong to make bullets without rigidbody?
I have a spacecraft flying in a straight tube very fast, and if i use physics, it isnt made for very fast bullets, it’s more for bouncing cars around. The bullets would take up too much processor, would miss collisions, because they dont need drag, rotation etc?
What is the computationally simple way to detect collider intersections?
Nobody answered you, but your question is a good one. Often using the rigidbodies in Unity takes precise control away from the developer. You may have already found your answer long ago, but for others like myself that wondered the same thing here is what works.
Set Projectile rigidbody to Is Kinematic in the inspector. This keeps it from being affected by physics but allows trigger collisions.
Set the colliders you intend for the projectile to hit to “Is Trigger”
For the code of shooting the bullet here is a sample of what I used (C#);
public float projSpeed = 12.0f;
void Fire ()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation);
//Ignore collision (Not sure if needed for kinematic)
Physics.IgnoreCollision(bullet.GetComponent<Collider>(), GetComponent<Collider>());
//Set speed of bullet
bullet.GetComponent<Bullet>().bulletSpeed = projSpeed;
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
For the Bullet itself;
public float bulletSpeed = 0.0f;
public Vector3 myDir;
public float speed = 30.0f; //Probably don't need a slerp for this
private Transform target;
void Start()
{
//Find object I want bullets to move towards, in this case it is my
//crosshairs on the screen "CHWave"
Transform nearestT; nearestT = GameObject.Find("CHWave").transform;
target = nearestT;
Vector3 vectorToTarget = target.position - transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
}
void Update()
{
//Move Projectile
transform.position += transform.right * bulletSpeed*Time.deltaTime;
}
void OnTriggerEnter(Collider other)//void OnCollisionEnter(Collision hit)
{
if (other.gameObject.tag == "PlayerTarget")// || hit.gameObject.tag == "Bullet")
{
// Physics.IgnoreCollision(other.GetComponent<Collider>(), GetComponent<Collider>());
Physics.IgnoreCollision(other.GetComponent<Collider>(), GetComponent<Collider>());
return;
}
var otherObj = other.gameObject;
var health = otherObj.GetComponent<HealthRTS>();
if (health != null)
{
health.TakeDamage(10);
}
Destroy(gameObject);
}
I use particles ever since…
You can control spread, simulate grain, speed, gravity, bouncing with speed loss, subemitters for splinters, sending message to collider for damage and decals… no spam of bullet-gameobjects while autofiring thousands of rounds. Now visualisation is easy, you can make tracers and much more.
Of course you can grab position and speed of every single particle… script-examples are in the web.
- Check if (bullet.position - gun.position) >= (target.position -
gun.position).
- Check if X/Z coordinate of the bullet is inside preset area (which is
target.position +/- its radius)
On both true the will be a headshot. Destroy, decrease HP and voilah.