Jump to content
  • entries
    44
  • comments
    19
  • views
    2321

Day 39 of 100 Days of VR: Creating a VR First Person Shooter II

Josh Chang

648 views

Welcome back to day 39!

Yesterday we started to look at fixing problems that involved the limitation of Mobile VR (and a lot of raycasting), today we’re going to make some more changes. Specifically, the goal today is:

  1. Change our Event Trigger logic to deal with what happens if we’re holding down on the screen
  2. Fix a problem with our player being pushed around
  3. Fix why our Knight turns black
  4. Change our enemy to be slower to make the game easier

Let’s get to it!

Step 1: Changing Our Event Trigger Logic for Continuous Fire

Right now, we’re trying to solve the problem where we only damage the enemies when we tap on them. If we were to hold on, then we would continue to shoot, but the enemies won’t take damage

Yesterday we discovered that there was 2 ways we could have implemented our game.

  1. Use our existing code where we shoot a raycast and depending on what we hit, run some function.
  2. Use the Event Trigger system along with Google’s changes.

I’ve played around quite a bit with the Event Trigger system and made a solution, but it’s not the best, in fact, I might have preferred just keeping what we have, but that’s okay, we’re just learning!

There are 2 problems that we must solve:

  1. What happens when we’re holding down the screen on an enemy
  2. What happens when we’re holding down and then we move to point at another enemy.

After playing around for a while, the PointerClick solution we have will no longer work.

Instead, I’ve started playing with the PointerEnter and PointerExit events.

I’m going to add the changes to EnemyHealth:

using System;
using UnityEngine;
using Random = UnityEngine.Random;

public class EnemyHealth : MonoBehaviour
{
    public float Health = 100;
    public AudioClip[] HitSfxClips;
    public float HitSoundDelay = 0.1f;

    private SpawnManager _spawnManager;
    private Animator _animator;
    private AudioSource _audioSource;
    private float _hitTime;
    private Boolean _isEnter;


    void Start()
    {
        _spawnManager = GameObject.FindGameObjectWithTag("SpawnManager").GetComponent<SpawnManager>();
        _animator = GetComponent<Animator>();
        _hitTime = 0f;
        _isEnter = false;
        SetupSound();
    }

    void Update()
    {
        _hitTime += Time.deltaTime;
        if (Input.GetButton("Fire1") && _isEnter)
        {
            TakeDamage(1);
        }
    }
	
    private void TakeDamage(float damage)
    {
        if (Health <= 0) { return; } if (_hitTime > HitSoundDelay)
        {
            Health -= damage;
            PlayRandomHit();
            _hitTime = 0;
        }

        if (Health <= 0)
        {
            Death();
        } 
    }

    private void SetupSound()
    {
        _audioSource = gameObject.AddComponent<AudioSource>();
        _audioSource.volume = 0.2f;
    }

    private void PlayRandomHit()
    {
        int index = Random.Range(0, HitSfxClips.Length);
        _audioSource.clip = HitSfxClips[index];
        _audioSource.Play();
    }

    private void Death()
    {
        _animator.SetTrigger("Death");
        _spawnManager.EnemyDefeated();
        foreach (Collider collider in GetComponentsInChildren<Collider>())
        {
            collider.enabled = false;
        }
    }

    public void HealthEnter()
    {
        _isEnter = true;
        print("enter");
    }

    public void HealthExit()
    {
        _isEnter = false;
        print("exit");
    }
}

Walking Through the Code

In our EnemyHealth, we create 2 new functions:

  • HealthEnter()
  • HealthExit()

These functions are going to be called from the PointerEnter and PointerExit from our Event Trigger that we set up.

In these functions, we set new variable we introduced called _isEnter so we know when they’re being selected.

  1. Inside Update() we check to see if we’re currently hovering over the enemies and if we’re pressing down on the screen. If we are, we would call our already existing Shoot function.

I’m not a fan of this method because it requires us to constantly call Update() in all of our enemy health scripts as opposed to just inside our PlayerShootingController script, but for just playing around, this is okay.

  1. I also changed the hit sound effect to be able to play every 0.1 seconds just like our shooting delay.

Step 1.1: Updating our Event Trigger

Now that we have our shooting script in, the next and final thing we need to do is to create the Event Triggers to use them.

Get rid of the PointerClick event that we’ve previously set up. Instead, we’re going to create 2 new types of Event Triggers: PointerExit and PointerEnter.

knight-enter-trigger.png

Here’s what we’re going to do:

  1. Attach HealthEnter() to PointerEnter
  2. Attach HealthExit() to PointerExit

Now we can play our game like we intended to do from the very beginning. Make sure to make these changes to the Bandit and Zombie too!

Step 2: Preventing Our Player from Falling

Currently, if we were to play the game, when an enemy gets closed to our player character, we would fall.

We should have addressed this in the past when we set constraints inside our RigidBody component, but it appears that we have not.

Let’s go back and fix this

  1. In the hierarchy, select Player
  2. Under the RigidBody component, we’re going to set our constraints. Specifically, we want to freeze our position and rotation so that the enemies won’t push our character around. Having the game move us could cause nausea for our players.

rigidbody.png

Step 3: Fixing our Knight’s Color

If we were to play our game on our mobile device, we’ll notice one big problem. Our knights are all black.

If we pay attention to our log, we’ll see this:

shader-probles.png

It seems that we have some problems with the shaders that the asset is using.

Unfortunately, I don’t know enough about this problem to resolve this. We have 2 choices:

  1. Ignore the problem and just have all black knights
  2. Change the materials that use these to use the standard shader.

In our case, we’re going to explore the 2nd option.

  1. In Assets/Knight/models/Materials we have 3 materials that we’re using: clothColor, knight1Color, weaponsColor, and all of them uses one of those 2 shaders above. Let’s select them and changed them to Standard.

change-shaders.png

Now if we were to play the game on Unity, here’s what the knights would look like:

new-enemy-knight.png

It lost the coloring we originally had for it, but at least we keep the details of the models.

Step 4: Making the Game Easier

Currently, in our game, we would be getting swarmed with enemies. That would have been fine if we can move around, unfortunately, we can’t do that anymore, thus as a result, we need to make some adjustments

We’re going to do 2 things:

  1. Change the rate of how long it takes for an enemy to spawn
  2. Slow down the rate our enemies move

Step 4.1: Changing Spawn Rate on the Spawn Manager

Currently, we spawn the next enemy every 2 seconds. Let’s change that to 5.

  1. Select the SpawnManager game object (child of GameManager)
  2. Set Time Between Enemies to be from 2 to 5

SpawnManager-time-between.png

Step 4.2: Changing Enemy Walking Speed

As we might recall, to control the enemy speed, we must look at the Nav Mesh Agent component in our enemy prefabs.

In order of speed, our speed order is Bandit, Knight, Zombie, with the Bandit being the fastest and Zombie being the slowest.

I’m going to change the speed a bit.

  1. Bandit to 2
  2. Knight to 1.5
  3. Zombie to 1

Here’s an example:

bandit-speed.png

Conclusion

Now we’re done! We have taken care of a lot of the technical problems that we encountered.

Tomorrow, we’re going to continue to finish the rest of the game by figuring out how we would add UI into a virtual reality environment.

Until then, I’ll see you all later in day 40!

Day 38 | 100 Days of VR | Day 40

Home



0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By BlackSpoon
      Hi guys, let me introduce my new project - Just Smash It! It's all about destruction! Break your way smashing objects with aimed shots!
      * Realistic physics of destruction
      * Smooth game flow
      * Pleasant graphic and sound design
      * Infinite mode after passing the basic set of levels
      * Small size, great time-killer!
      Play Market: https://play.google.com/store/apps/details?id=com.blackspoongames.smashworld
      Feedback are welcome!
    • By Rio Lloyd
      Hey all!
      we are a team of 3 looking for more members, 
      we are making an isometrical Survival RPG.
      we are looking For Members who can make low poly 3D artists who can do character models, environments, tools and more.
       
      if interested and want to know more email me at rioishere14@gmail.com
    • By nxrighthere
      BenchmarkNet is a console application for testing the reliable UDP networking solutions.
      Features:
      Asynchronous simulation of a large number of clients Stable under high loads Simple and flexible simulation setup Detailed session information Multi-process instances Supported networking libraries:
      ENet UNet LiteNetLib Lidgren MiniUDP Hazel Photon Neutrino DarkRift More information and source code on GitHub.
      You can find the latest benchmark results on the wiki page.
       
    • By trapazza
      I'm trying to add some details like grass, rocks, trees, etc. to my little procedurally-generated planet. The meshes for the terrain are created from a spherified cube which is split in chunks (chunked LOD).
      To do this I've wrote a geometry shader that takes a mesh as input and uses its vertex positions as locations where the patches of grass will be placed (as textured quads).
      For an infinite flat world (not spherical) I'd use the terrain mesh as input to the geometry shader, but I've found that this won't work well on a sphere, since the vertex density is not homogeneous across the surface.
      So the main question would be: How to create a point cloud for each terrain chunk whose points were equally distributed across the chunk?
      Note: I've seen some examples where these points are calculated from intersecting a massive rain of totally random perpendicular rays from above... but I found this solution overkill, to say the least.
      Another related question would be: Is there something better/faster than the geometry shader approach, maybe using compute shaders and instancing?
    • By FedGuard
      Hello all,
       
      I would like to start off with thanking you all for this community. Without fora like these to assist people the already hard journey to making an own game would be exponentially more difficult. Next I would like to apologize for the long post, in advance...
      I am contemplating making a game. There, now that's out of the way, maybe some further details might be handy.
      I am not some youngster (no offence) with dreams of breaking into the industry, I am 38, have a full-time job, a wife, kid and dog so I think I am not even considered indie? However I recently found myself with additional time on my hands and decided I would try my hand at making a game.Why? Well mostly because I would like to contribute something, also because I think I have a project worth making (and of course some extra income wouldn't hurt either to be honest). The first thing I realized was, I have absolutely no relevant skill or experience. Hmm; ok, never mind, we can overcome that, right?
      I have spent a few months "researching",meaning looking at YouTube channels, reading articles and fora. Needless to say, I am more confused now than when I started. I also bought some courses (Blender, Unity, C#) and set out to make my ideas more concrete.
      I quickly discovered, I am definitely not an artist... So I decided, though I do plan to continue learning the art side eventually, I would focus on the design and development phase first. The idea being, if it takes me a year or more solely learning stuff and taking courses without actually working on my game, I would become demoralized and the risk of quitting would increase.
      So I thought I would:
      1: Keep following the courses Unity and C# while starting on the actual game development as the courses and my knowledge progress.
      2: Acquire some artwork to help me get a connection with the game and main character, and have something to helm keep me motivated. (I already did some contacting and realized this will not be cheap...). Also try to have the main character model so I can use it to start testing the initial character and game mechanics. For this I have my first concrete question. I already learned that outsourcing this will easily run up in the high hundreds or thousands of dollars... (lowest offer so far being 220 USD) I am therefore playing with the idea of purchasing https://assetstore.unity.com/packages/3d/animations/medieval-animations-mega-pack-12141 with the intention of then have an artist alter and/or add to the animations (it is for a Roman character so some shield animations are not going to work the same way.). This way I could start  with the basic character mechanics. Is this a good idea, waste of money,...? Any suggestions? I then have a related but separate question. Is it a good idea to buy Playmaker (or some other similar software I haven't yet heard of like RPGAIO), and using this for initial build, then changing/adding code as the need arises?
      3.Get a playable initial level ready as a rough demo and then starting to look for artist for level design and character/prop creation.
      ...
       
      I would really appreciate some input from more experienced people, and especially answers to my questions. Of course any advice is extremely welcome.
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!