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:
Change our Event Trigger logic to deal with what happens if we’re holding down on the screen
Fix a problem with our player being pushed around
Fix why our Knight turns black
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.
Use our existing code where we shoot a raycast and depending on what we hit, run some function.
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:
What happens when we’re holding down the screen on an enemy
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.
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.
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.
Here’s what we’re going to do:
Attach HealthEnter() to PointerEnter
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
In the hierarchy, select Player