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 // reset the timer that manages the firing timing of bullets // fire a bullet // function to fire a bullet // fire multiple bullets // generate a bullet to fire // set the direction and speed at which the bullet is fired // fire only one bullet // set the direction and speed at which the bullet is fired // function to take damage // reduce HP // If i still have HP, stop here // hide because player died /// // Function to increase experience // If i don't have enough experience to level up, end here // Level up // Remember the experience value needed for this level up // Calculate experience required for next level up // Activate Bomb when leveling up // Since we have leveled up, update various parameters // A function that calculates the experience required for the specified level I would appreciate it if you could explain in detail what is lacking if there is something lacking.
if (m_shotTimer
m_shotTimer = 0;
ShootNWay (angle, m_shotAngleRange, m_shotSpeed, m_shotCount);
}
private void ShootNWay (
float angleBase, float angleRange, float speed, int count)
{
var pos = transform.localPosition;// player position
var rot = transform.localRotation;// player orientation
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);
var shot = Instantiate (m_shotPrefab, pos, rot);
shot.Init (angle, speed);
}
}
else if (count == 1)
{
// generate a bullet to fire
var shot = Instantiate (m_shotPrefab, pos, rot);
shot.Init (angleBase, speed);
}
}
// Called when you hit an enemy
public void Damage (int damage)
{
// play SE when damaged
var audioSource = FindObjectOfType ();
audioSource.PlayOneShot (m_damageClip);
m_hp-= damage;
if (0
// 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);
}
}
// called when you get the gem
public void AddExp (int exp)
{
// increase experience
m_exp + = exp;
if (m_exp
m_level ++;
// (for use in displaying the experience gauge)
m_prevNeedExp = m_needExp;
m_needExp = GetNeedExp (m_level);
// 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);
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
}
private int GetNeedExp (int level)
{
/ *
* /
return m_nextExpBase +
m_nextExpInterval * ((level-1) * (level-1));
}
}
unity uses 2018.4.20f1.
-
Answer # 1
Related articles
- unity - i want to black out the black and white texture used for the effect without making it additive
- Unity uses camera to achieve telescope effect
- Unity implements game card scroll effect
- Unity achieves card flip effect
- Unity achieves background image fade in and fade out effect
- Unity shader to achieve the mask effect
- Unity achieves marquee draw effect
- Unity achieves 3D circular scroll effect
- Gaussian blur effect with Unity shader
- Unity shader achieves free zoom effect
- Unity shader for ablation effect
- Unity to achieve the effect of writing on the screen
- Unity UI or 3D scene to achieve the effect of following the mobile phone's gyroscope
- Unity Shader achieves book turning effect
- [unity/oculus go] i want to apply post processing effect only to specific objects
- Unity implements UI halo effect (glow effect)
- JS achieve fireworks explosion effect
- Unity achieves virtual rocker effect
- Unity realizes the glow effect of the edge of the character's attacked body
- c # - player production in unity (shooting game)
- c # - unity) i want to get the image size in prefab
- a question about unity please tell me how to display animatorsettrigger in onclick() of button
- c# - [unity] hit detection of tile map after adding composite collider 2d
- c# - method detection by instantiated gameobjects
- c # - [unity 2d] i want to create a gimmick that clings to a rope, hangs, swings like a pendulum, and jumps
- i want the button to proceed to the next in unity to appear when the game clear is displayed in the ui
- how do i place sprites responsively in unity2d?
- c # - unity) video thumbnails
- c # - unity) show/hide prefab button conditionally
if (collision.name.Contains ("Ememy"))
Enemy instead of Ememy?