Home>

I am making a shooting game with reference to "26 times of making omnidirectional shooting" on the net. When I try to do "not display in this article, show explosion effect when player hits enemy and get damage", no program error is displayed and no effect is displayed. Since C # and unity have just begun, there may be some mistakes in the rudimentary part. Thanks for your response.

// using System.Collections;
// using System.Collections.Generic;
using UnityEngine;

public class Player: MonoBehaviour
{
public float m_speed;// speed of movement
public shot m_shotPrefab;// bullet prefab

///
/// add to
///
public Explosion m_explosionPrefab;// Explosion effect prefab

public float m_shotSpeed;// speed of bullet movement
public float m_shotAngleRange;// Angle when firing multiple bullets
public float m_shotTimer;// timer that manages the firing timing of bullets
public int m_shotCount;// number of shots fired
public float m_shotInterval;// bullet firing interval (seconds)
public int m_hpMax;// HP maximum value
public int m_hp;// HP
// static variable that manages the player instance
public static Player m_instance;
public float m_magnetDistance;// Distance to attract gems
public int m_nextExpBase;// Base value of experience required until the next level
public int m_nextExpInterval;// Experience value increase required to the next level
public int m_level;// level
public int m_exp;// experience value
public int m_prevNeedExp;// EXP required for previous level
public int m_needExp;// EXP required for next level
public AudioClip m_levelUpClip;// Play when level up SE
public AudioClip m_damageClip;// SE to play when damaged
public int m_levelMax;// maximum level
public int m_shotCountFrom;// Number of shots fired (when level is at minimum)
public int m_shotCountTo;// Number of shots (when level is at maximum)
public float m_shotIntervalFrom;// bullet firing interval (seconds) (when level is at minimum)
public float m_shotIntervalTo;// bullet firing interval (seconds) (when level is at maximum)
public float m_magnetDistanceFrom;// Distance to attract gems (when level is at minimum)
public float m_magnetDistanceTo;// Distance to attract gems (at maximum level)

// Function called when the game starts
private void Awake ()
{
// Allow players to be referenced by other classes
// Store instance information in static variable
m_instance = this;
m_hp = m_hpMax;// HP
m_level = 1;// level
m_needExp = GetNeedExp (1);// EXP required for next level

// Play SE when level up
var audioSource = FindObjectOfType ();
audioSource.PlayOneShot (m_levelUpClip);

m_shotCount = m_shotCountFrom;// number of shots fired
m_shotInterval = m_shotIntervalFrom;// bullet firing interval (seconds)
m_magnetDistance = m_magnetDistanceFrom;// Distance to attract gems
}

void Update ()
{
// Get the input information of the arrow keys
var h = Input.GetAxis ("Horizontal");
var v = Input.GetAxis ("Vertical");

// move the player in the direction the arrow keys are pressed
var velocity = new Vector3 (h, v) * m_speed;
transform.localPosition + = velocity;

// limit the position so that the player does not go out of the screen
transform.localPosition = Utils.ClampPosition (transform.localPosition);

// calculate the player's screen coordinates
var screenPos = Camera.main.WorldToScreenPoint (transform.position);

// Calculate the direction of the mouse cursor as seen by the player
var direction = Input.mousePosition-screenPos;

// get the angle of the direction the mouse cursor is in
var angle = Utils.GetAngle (Vector3.zero, direction);

// make the player look at the direction of the mouse cursor
var angles = transform.localEulerAngles;
angles.z = angle-90;
transform.localEulerAngles = angles;

// Update the timer that manages the firing timing of bullets
m_shotTimer + = Time.deltaTime;

// If it's not the time to fire the bullet, end the process here
if (m_shotTimer

// reset the timer that manages the firing timing of bullets
m_shotTimer = 0;

// fire a bullet
ShootNWay (angle, m_shotAngleRange, m_shotSpeed, m_shotCount);
}

// function to fire a bullet
private void ShootNWay (
float angleBase, float angleRange, float speed, int count)
{
var pos = transform.localPosition;// player position
var rot = transform.localRotation;// player orientation

// fire multiple bullets
if (1 {
// loop as many times as you fire
for (int i = 0;i {
// calculate the firing angle of the bullet
var angle = angleBase +
angleRange * ((float) i/(count-1)-0.5f);

// generate a bullet to fire
var shot = Instantiate (m_shotPrefab, pos, rot);

// set the direction and speed at which the bullet is fired
shot.Init (angle, speed);
}
}

// fire only one bullet
else if (count == 1)
{
// generate a bullet to fire
var shot = Instantiate (m_shotPrefab, pos, rot);

// set the direction and speed at which the bullet is fired
shot.Init (angleBase, speed);
}
}

// function to take damage
// Called when you hit an enemy
public void Damage (int damage)
{
// play SE when damaged
var audioSource = FindObjectOfType ();
audioSource.PlayOneShot (m_damageClip);

// reduce HP
m_hp-= damage;

// If i still have HP, stop here
if (0

// hide because player died
// Originally, play the game over effect here
gameObject.SetActive (false);
}

///
/// add to
///
private void OnTriggerEnter2D (Collider2D collision)
{
if (collision.name.Contains ("Ememy"))
{
// Generate an explosion effect where you hit the enemy
Instantiate (m_explosionPrefab, collision.transform.localPosition, Quaternion.identity);
}
}

// Function to increase experience
// called when you get the gem
public void AddExp (int exp)
{
// increase experience
m_exp + = exp;

// If i don't have enough experience to level up, end here
if (m_exp

// Level up
m_level ++;

// Remember the experience value needed for this level up
// (for use in displaying the experience gauge)
m_prevNeedExp = m_needExp;

// Calculate experience required for next level up
m_needExp = GetNeedExp (m_level);

// Activate Bomb when leveling up
// The number of bombs fired and the speed are defined by fixed hits
// make it a public variable if necessary,
// Please change so that it can be set on the Unity editor
var angleBase = 0;
var angleRange = 360;
var count = 28;
ShootNWay (angleBase, angleRange, 0.15f, count);
ShootNWay (angleBase, angleRange, 0.2f, count);
ShootNWay (angleBase, angleRange, 0.25f, count);

// Since we have leveled up, update various parameters
var t = (float) (m_level-1)/(m_levelMax-1);
m_shotCount = Mathf.RoundToInt (
Mathf.Lerp (m_shotCountFrom, m_shotCountTo, t));// number of shots fired
m_shotInterval = Mathf.Lerp (
m_shotIntervalFrom, m_shotIntervalTo, t);// bullet firing interval (seconds)
m_magnetDistance = Mathf.Lerp (
m_magnetDistanceFrom, m_magnetDistanceTo, t);// distance to attract gems
}

// A function that calculates the experience required for the specified level
private int GetNeedExp (int level)
{
/ *

  • For example, if m_nextExpBase is 16 and m_nextExpInterval is 18,
  •  
  • Level 1: 16 + 18 * 0 = 16
  • Level 2: 16 + 18 * 1 = 34
  • Level 3: 16 + 18 * 4 = 88
  • Level 4: 16 + 18 * 9 = 178
  •  
  • It becomes such a calculation formula, and the required experience value increases as the level increases
    * /
    return m_nextExpBase +
    m_nextExpInterval * ((level-1) * (level-1));
    }
    }

I would appreciate it if you could explain in detail what is lacking if there is something lacking.
unity uses 2018.4.20f1.