First Person Game Update
- Keshav Batra
- Feb 18, 2021
- 1 min read
Updated: Mar 2, 2021
What I did was create a way for the weapon to fire bullets. Unfortunately the bullets aren't able to shoot from the gun since the light doesn't fire from the muzzle.
Here's some of the code used to make the gun shoot:
{
public Transform Barrel;
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public Camera fpsCam;
public GameObject muzzleFlash;
public GameObject impactEffect;
public GameObject BarrelObject;
private float nextTimeToFire = 0f;
// Update is called once per frame
void Update()
{
Debug.Log(transform.position.x +" " + transform.position.y);
if (Input.GetButton("Fire1")&& Time.time>= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot()
{
muzzleFlash.GetComponent<ParticleSystem>().Play();
//GameObject muzzleParticle= Instantiate(muzzleFlash, BarrelObject.transform.position, BarrelObject.transform.rotation);
//muzzleParticle.transform.parent = transform.parent;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
Yorumlar