14. Handling of Weapon Attacks
1 Inherit the ATKandDamage() method and override its properties
2 Handling the Event event of the animation state machine
3 Simple interest mode (it is easier to get the List collection)
Public static SpawnManager _instance;
Void Awake(){
_instance=this;
}
4 Attack the Moster with the shortest distance from the Player
Public void AttackA(){
GameObject go=null;//Store the shortest attack target
Float diatance =attackDistance;
//Only one SpawnManager is generated
Foreach(GameObject go in SpawnManager._instance.eremyList){
Vector3 temp=Vector3.Disance(this,go.transform.position);
If(temp<distance){
Distance=temp; //Store the shortest distance
Enemy=go; //Store the shortest attack target
}
}
If(enemy!=null){
//Player is facing enemy, monster's blood is reduced
Vector3 targetPos=enemy.transform.position;
Transform.position.y=targetPos.y;
transform.lookAt(targetPos);
Enemy.GetComponent<ATKandDamage>().TakeDamage(normalAttack);
}
}
5 Functions that deal with guns (bullet walking and bullet detection)
Add a rigid body component, which is not affected by gravity
1 Listen to the bullet's OnTriggerEnter event and process the monster's blood loss
2 Instances of bullets
// follow the rotation of the root object
GameObject go = GameObject.Instantiate(bullPrefabs,bulletPos,transform.root.rotation);
go.GetComponent<Bullet>().attack=attack;
15. Dealing with the effect of death
anim.SetBool("Death", true);
EnemyManager._instance.enemyList.Remove(this.gameObject);
GameObject.Destroy(this.gameObject, 1f);
this.GetComponent<CharacterController>().enabled = false;
Set up a temporary List collection in the attack event to see the storage of eligible monsters
public void AttackRange()
{
List < GameObject > enemyList = new List < GameObject >(); //Create an intermediary collection to prevent monster removal from conflicting with collection removal
//Monsters with distance less than AttackDIstance are injured
foreach (GameObject go in EnemyManager._instance.enemyList)
{
float temp = Vector3.Distance(transform.position, go.transform.position);
if(temp<attackDistance)
{
enemyList.Add(go);
//go.GetComponent<ATKandDamage>().TakeDanmage(attackRange);
}
}
foreach(GameObject go in enemyList)
{
go.GetComponent<ATKandDamage>().TakeDanmage(attackRange);
}
}
16. Develop dropped items
1 Create the award item class AwardItem
public enum AwardType
{
Gun,
DualSword
}
public class AwardItem : MonoBehaviour {
public AwardType type;
void Start()
{
//Set the angular velocity of the rigid body
this.GetComponent<Rigidbody>().velocity=new Vector3(Random.Range(-5, 5), 0, Random.Range(-5, 5));
}
}2 Collision detection OnColliderEnter(){}
stop motion upon contact with the ground
Cancel Gravity Use Dynamics Set as Trigger
void OnCollisionEnter(Collision collision)
{
if(collision.collider.tag==Tags.ground)
{
rig.useGravity = false;
rig.isKinematic = true;
sCollider.isTrigger = true;
sCollider.radius = 2;
}
}
//Realize touching the protagonist, the reward item starts to move, and the protagonist moves
3 Trigger detection OnTriggerEnter(Collider collider){}{
If(collider.tag==Tags.player){
startMove==true;
Player=col.transform;
}
}
New knowledge in practice: Create a separate class for reward items, use fields, and use the Prefab for each prize
Seventeen, weapon switching
1 Spawn reward item SpawnAward()
1) Handle spawn after game object dies
2) Use random numbers to handle the generation of reward items
3) Existence time of reward items
2 Logic of weapon switching class (to enable weapons according to AwardType)
Nineteen, the switch of the attack button
1 Use the singleton mode to toggle the attack panel
Called when switching weapons
20. Making a minimap
1 Create a small map UI, use the parent object, and make the small map rotate according to the set point
2 Create a class for the minimap, set a singleton to get the Sprits of the minimap
float y = transform.eulerAngles.y; //Get the player's rotation on the y-axis
playerIcon.localEulerAngles = new Vector3 (0, 0, y);
3 Enemies are automatically generated, you need to know how to create icons dynamically
Public void BossIcon(){
GameObject go= NGUITools.AddChild(this.gameobject,enemyIconPrefab);
go.GetComponent<UISprite>().spriteName=”BossIcon”; //Change the icon
Return go;
}
4 Create EnemyIcon() on the monster
1 Obtain the UI position of the dynamically created icon separately
2 Create a coordinate difference centered on the protagonist
Vector offset=transfome.position-player.position;
icon.localPosition=new Vector(offset.x,offset.z,0);
Twenty-one, add sound
1 Add AudioListener to the protagonist and add AudioSoures to the main camera
AudioSource.PlayClipAtPoint(deathClip, transform.position, 1f);
Twenty-two, the production of weapon drag marks effect
Using weapon plugins