Hey guys, I’m still working on my gun script here.
So i am just now attempting to learn about sending out projectiles when I shoot my gun.
Essentially my error here is when I shoot my objects take damage and force before the projectile even hits them.
If anyone would be so kind as to inspect my script and inform me on why it is my projectiles do not interact correctly with the world I’d be greatly appreciative.
Also as a side note, how would I delete the bullet prefab upon impact with objects?
Just wanna say thanks again for any help that comes my way. <3
Below is my gun script currently.
using UnityEngine;
public class Carbine : MonoBehaviour
{
public int minimumDamage = 5;
public int maximumDamage = 15;
public int maximumRange = 50;
public float fireRate = .05f;
public float impactForce = 30f;
public float projectileSpeed = 1000f;
private float nextFire = 0f;
public ParticleSystem MuzzleFlash;
public Camera MainCamera;
public GameObject ImpactEffect;
public GameObject MainProjectile;
public GameObject ProjectileEmitter;
void FixedUpdate()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
Shoot();
}
}
void Shoot()
{
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(MainProjectile, ProjectileEmitter.transform.position, ProjectileEmitter.transform.rotation) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.forward * projectileSpeed);
Destroy(Temporary_Bullet_Handler, 1f);
MuzzleFlash.Play();
nextFire = Time.time + fireRate;
MuzzleFlash.Play();
nextFire = Time.time + fireRate;
RaycastHit hit;
if (Physics.Raycast(MainCamera.transform.position, MainCamera.transform.forward, out hit, Mathf.Infinity))
{
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
Instantiate(ImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
GameObject go = Instantiate<GameObject>(ImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
IDamageable damageable = hit.collider.GetComponent<IDamageable>();
nextFire = Time.time + fireRate;
if (damageable != null)
{
float normalisedDistance = hit.distance / maximumRange;
if (normalisedDistance <= 1)
{
damageable.DealDamage(Mathf.RoundToInt(Mathf.Lerp(maximumDamage, minimumDamage, normalisedDistance)));
}
}
}
}
}
And below is my gun script without my code for adding bullets.
using UnityEngine;
public class Carbine : MonoBehaviour
{
public int minimumDamage = 5;
public int maximumDamage = 15;
public int maximumRange = 50;
public float fireRate = .05f;
public float impactForce = 30f;
private float nextFire = 0f;
public ParticleSystem MuzzleFlash;
public Camera MainCamera;
public GameObject ImpactEffect;
void FixedUpdate()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
Shoot();
}
}
void Shoot()
{
MuzzleFlash.Play();
nextFire = Time.time + fireRate;
RaycastHit hit;
if (Physics.Raycast(MainCamera.transform.position, MainCamera.transform.forward, out hit, Mathf.Infinity))
{
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
Instantiate(ImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
GameObject go = Instantiate<GameObject>(ImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
IDamageable damageable = hit.collider.GetComponent<IDamageable>();
nextFire = Time.time + fireRate;
if (damageable != null)
{
float normalisedDistance = hit.distance / maximumRange;
if (normalisedDistance <= 1)
{
damageable.DealDamage(Mathf.RoundToInt(Mathf.Lerp(maximumDamage, minimumDamage, normalisedDistance)));
}
}
}
}
}