100 Days of VR: Day 9 Setting up a Weapon

Adding our Weapon

In our current state, we just have our character:

Creating our weapon

The first thing we should do is create our “gun”. I’m sure we can grab a gun asset from the game store, but instead, I’m going to first create a simple cube that’ll represent our weapon. I’m sure later on we can just attach the asset and *most likely* everything will be fine. Probably…

  • Position: (0.25, -0.5, 1)
  • Scale: (0.25, 0.25, 1)
  • Duration: 1
  • Looping: unchecked
  • Start Lifetime: 0.05
  • Start Speed: 5
  • Start Size: 1
  • Start Color: Yellow
  • Play Awake: unchecked

Adding the Shooting Script

Next up, I created the shooting script and attached it to our Main Camera. The script will be called PlayerShootingController.

using UnityEngine;public class PlayerShootingController : MonoBehaviour
{
public float Range = 100;
private Camera _camera;
private ParticleSystem _particle;
void Start () {
_camera = Camera.main;
_particle = GetComponentInChildren<ParticleSystem>();
}

void Update () {

if (Input.GetMouseButton(0))
{
Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, Range))
{
print("hit " + hit.collider.gameObject);
_particle.Play();
}
}
}
}

End of Day 8

That’s it for today! Short? Yeah I know, great right?

--

--

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