100 Days of VR: Day 16 Using A Real Gun Model In Unity

  1. Use a real gun asset
  2. Add the shooting effect back in
  3. Create code to trigger reloading

Setting Up a Real Gun Asset

  • Position: (0.25, -0.5, 0.75)
  • Rotation: (0. 90, 0)

Setting up the Gun Animation to Run When We Shoot

  • DoOpen
  • DoReload
  • Shoot
  • Default
  • MachingGun_open
  • MachineGun_shoot
  • MachineGun_reload
  • Shoot transition to the MachineGun_shoot
  • DoOpen transitions to MachineGun_open
  • DoReload transitions to MachineGun_reload
  • Shoot transition back to default
  • DoOpen transitions to MachineGun_open
  • DoReload transitions to MachineGun_reload
  • Shoot transition to the MachineGun_shoot
  • DoOpen transitions back to default
  • DoReload transitions to MachineGun_reload
  • After the animation time is finish we transition to default

Creating the Code to Shoot

  • Range: 100
  • Shooting Delay: 0.2
  • Shot Sfx Clips: Machine_Gunfire_01
  1. Hit reload while we’re not doing anything
  2. Hit reload while we’re shooting
  3. Shoot while we’re reloading
  4. Never let go of our mouse before we shoot, reload, and then hold on until after we’re done reloading
using UnityEngine;
public class PlayerShootingController : MonoBehaviour
{
public float Range = 100;
public float ShootingDelay = 0.1f;
public AudioClip ShotSfxClips;
private Camera _camera;
private ParticleSystem _particle;
private LayerMask _shootableMask;
private float _timer;
private AudioSource _audioSource;
private Animator _animator;
private bool _isShooting;
private bool _isReloading;
void Start () {
_camera = Camera.main;
_particle = GetComponentInChildren<ParticleSystem>();
Cursor.lockState = CursorLockMode.Locked;
_shootableMask = LayerMask.GetMask("Shootable");
_timer = 0;
SetupSound();
_animator = GetComponent<Animator>();
_isShooting = false;
_isReloading = false;
}

void Update ()
{
_timer += Time.deltaTime;
if (Input.GetMouseButton(0) && _timer >= ShootingDelay && !_isReloading)
{
Shoot();
if (!_isShooting)
{
TriggerShootingAnimation();
}
}
else if (!Input.GetMouseButton(0))
{
StopShooting();
if (_isShooting)
{
TriggerShootingAnimation();
}
}
if (Input.GetKeyDown(KeyCode.R))
{
StartReloading();
}
}
private void StartReloading()
{
_animator.SetTrigger("DoReload");
StopShooting();
_isShooting = false;
_isReloading = true;
}
private void TriggerShootingAnimation()
{
_isShooting = !_isShooting;
_animator.SetTrigger("Shoot");
}
private void StopShooting()
{
_audioSource.Stop();
_particle.Stop();
}
private void Shoot()
{
_timer = 0;
Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
_audioSource.Play();
_particle.Play();
if (Physics.Raycast(ray, out hit, Range, _shootableMask))
{
print("hit " + hit.collider.gameObject);
EnemyHealth health = hit.collider.GetComponent<EnemyHealth>();
EnemyMovement enemyMovement = hit.collider.GetComponent<EnemyMovement>();
if (enemyMovement != null)
{
enemyMovement.KnockBack();
}
if (health != null)
{
health.TakeDamage(1);
}
}
}
// called from the animation finished
public void ReloadFinish()
{
_isReloading = false;
}
private void SetupSound()
{
_audioSource = gameObject.AddComponent<AudioSource>();
_audioSource.volume = 0.2f;
_audioSource.clip = ShotSfxClips;
}
}
  1. We created more variables to keep track of what state we’re in. Currently we’re using Booleans, however I think using Enums to represent our state might be a better idea, especially if we have more states to keep track of.
  2. In Start() we initialize our new boolean states
  3. In Update(), while we’re not reloading and we hit Shoot, if we haven’t started shooting, we’ll start our shooting animation in TriggerShootingAnimation() and we’ll run our shooting logic in Shoot()
  4. Whenever we let go of our mouse, we’ll go to the else statement in Update() and stop our shooting animation and run our stop shooting logic in StopShooting() which stops our music and particle effect
  5. A side note: in Shoot(), I started the particle effect when we hit something with the raycast, we want it to always run whenever we shoot.
  6. In Update() when we want to reload, we press R and we’ll go into StartReloading(), here we would trigger our reload animation to start and disable our shooting so the players can’t continue shooting.
  7. We have a public function ReloadFinish() that tells us we’re down reloading. We’re going to have to go to the animation for our machine gun and add an event to run this function whenever we’re done reloading.
  1. Select MachineGun_reload as our animation
  2. Go to frame 68
  3. Click the add event button below our frame number
  4. Select ReloadFinish() as the function to run once we get to that frame

Conclusion

--

--

--

Software Engineer by day, side hustler wannabee by night! https://leetdev.io

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to use Kaggle API in Google Colab

Image showcasing different Street Fighter character combos

Chrome OS Apps: Dabble Writer

Effects of Business Analysis on Software Project Outcomes

DATA STRUCTURES.

Develop your django application within a docker container

S.O.L.I.D: The First 5 Principles of Object Oriented Design

Introduction to Qwiklabs

Observer Pattern in Game Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Josh Chang

Josh Chang

Software Engineer by day, side hustler wannabee by night! https://leetdev.io

More from Medium

Cinemachine and Timeline — Camera Control in Unity

Unity Counterparts in Unreal Engine

Occlusion Culling

⚙️Weekly Gearbox #000