Day 3 of Getting Started with Unity: Bullets, Triggers, Tabs, and Invincible Death Animations
Posted onEdited on
This article describes the firing code of the bullet, the different triggers when the bullet hits different objects, and the animation of the character’s death and invincibility time.
//In the player file: //Refer to the bullet's model public GameObject bulletPrefab; //Setting parameters // bullet rotation degrees private Vector3 bulletEulerAngles; // timer privatefloat timeVal;
//Add the tank rotation to the previous code that controls the tank's orientation: //The 2D game is reversed, and it rotates 180 degrees along the x-axis. "The 2D game and UI should be reversed (positive 90 degrees -> negative 90 degrees)" if (h < 0){ sr.sprite = tankSprite[3]; //to the left bulletEulerAngles = new Vector3(0,0,90); } elseif (h > 0){ sr.sprite = tankSprite[1]; //rotate right bulletEulerAngles = new Vector3(0,0,-90); } if (v < 0){ sr.sprite = tankSprite[2]; //under bulletEulerAngles = new Vector3(0,0,-180); } elseif (v > 0){ sr.sprite = tankSprite[0]; //Up; the original image is facing up, and it does not need to be set to 180, otherwise it will be the same as the image facing down bulletEulerAngles = new Vector3(0,0,0); }
//The attack method of the tank privatevoidAttack(){ //if the player presses the space bar if(Input.GetKeyDown(KeyCode.Space)) { //Instantiate the bullet (game object, tank position, rotation angle) //The angle generated by the bullet: the angle of the current tank + the angle the bullet should rotate //Rotation in the panel is a quaternion converted to Euler angles, not rotation itself. //Use API:Quaternion.Euler to convert quaternion to rotation Instantiate(bulletPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bulletEulerAngles)); // reset the timeVal timer timeVal = 0; } }
//Bullet shooting interval (attack cd) voidUpdate(){ //attack cd // Usually timers are placed in Update() if(timeVal >= 0.4f){ Attack(); } //If there is no 0.4f, it will increase with time, so as to achieve the effect of attack interval else{ timeVal += Time.deltaTime; } }
//Finally, set the rate of fire and movement method in the bullet file: publicfloat movespeed = 10; voidUpdate() { //Not transform.forward, forward is moving along the z axis //Should use .up to achieve x-axis movement //Translate method with two parameters: the first parameter can be moved along the world coordinate axis, //Or move in its own direction, //If it is the world, the second parameter does not need to be filled, //You can also choose Space.World or Space.Self, //Otherwise you must fill in Space.World transform.Translate(transform.up * movespeed * Time.deltaTime, Space.World);
Triggers and tags
As we all know, when a bullet hits a wall in a tank battle, it should collide with the wall and disappear. So we set the bullet trigger here: both sides need to have a collider component (Box Collider 2D), one of them must have a trigger (is trigger☑️), and the moving side has a rigid body (Rigidbody 2D ) and set his gravity to 0.
When different objects collide with the bullet, we hope that there will be different results: for example, when the bullet hits the player, it will explode and cause the player to die; when the bullet hits the grass, it will pass through, etc. We can set tags (tag) for convenience:
In the bullet script, we use switch to build the trigger frame:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//Use the 2D trigger collision->the object hit by the bullet publicvoidOnTriggerEnter2D(Collider2D collision){ switch(collision.tag){ case"Tank": collision.SendMessage("Die"); break; case"Heart": break; case"Barrier": break; case"Wall": break; case"Enemy": break; default: break; }
Invincibility and Death
death function
1 2 3 4 5 6 7 8 9 10 11 12 13
//Refer to the death animation public GameObject explosionPrefab; // Tank's death method privatevoidDie(){ //Detect whether the player is in a protected state, if so, return directly without performing the following death effects if(isDefended == true){ return; } //generate explosion effect Instantiate(explosionPrefab, transform.position, transform.rotation); //die Destroy(gameObject); }
//Set the invincibility state privatebool isDefended = true;//The default value of boolean is false //Set the invincible time accumulator privatefloat defendTimeVal = 3;
//Refer to the invincible animation public GameObject defendEffectPrefab; //Set invincibility in the Update main program: voidUpdate() { //Whether it is invincible if(isDefended){ //SetActive directly manipulates the display and closing of the image defendEffectPrefab.SetActive(true); //The protection time is decremented defendTimeVal -= Time.deltaTime; //If 3 seconds of invincibility has elapsed, the animation is turned off, and the invincibility state is false if(defendTimeVal <= 0){ isDefended = false; defendEffectPrefab.SetActive(false); } }} }