Shooting&Collider — UnityGameDebut
ShootingAtUs & CapsuleCollider & Raycast— Episode #08
Hi, in the previous post we’ve got both enemies moving randomly (check for the previous code in Github). Now let’s make them shoot at us.
Step #01 — In the Unity app 1) select the Soldier(1) and in the Animator tab; 2) add the animation from Asset > ToonSoldiers_demo> animation > assault_combat_shoot 3) by dragging it to Animator; 4) right mouse click assalt_combat_idle and choose Make Transition and 5) drag it to assault_combat_shoot;
Step #02 — Select the animation in the Project view (under scene view) and in the Inspector click 1) Edit, then check the 2) Loop Time checkbox and press 3) Apply button;
Step #03 — Select 1) the Transition (idle ☞ shoot), 2) click + button and add Bool shoot; 3) on Inspector click + button and 4) add shoot = true; this is the condition for the transition selected occurs; 5) disable Has Exit Time;
Step #04 — make the same to 1) the inverse transition (shoot ☞ idle), but now 2) choose shoot = false; we’ll control the right time our enemies must shoot at us;
Step #05 — Open Soldier.cs, and add a property to get access to the Animator object:
private Animator mAnimator = null;
Step #06 — In the Soldier.Awake() and below mEnemy=transform… type the code below:
note: if you put mAnimator before the position of mEnemy it won’t work…
We will change the times of these 3 variables too (in Inspector you can experiment with these values):
public float RightTime = 3.0f;
public float ShootTime = 1.0f;
public float LeftTime = 2.0f;//Get the enemy actual gameObjectmEnemy = transform.GetChild(0).gameObject;mAnimator = mEnemy.GetComponent<Animator>();
Step #07 — In the Soldier.Activate() type (this boolean controls the idle ☞ shooting transition animation):
mAnimator.SetBool("shoot", true);
Step #08 — In the Soldier.MoveRightwards() type this (this activates back shooting ☞ idle transition):
mAnimator.SetBool("shoot", false);
Step #09 — In the Animator select assault_combat_shoot animation and set:
Speed = 2
This will speed up the shooting animation (adjust at your flavor);
On Visual Studio click SaveAll.
Run the animation;
The enemies are now shooting at us!
Good!
Now, let's add a Capsule Collider so that the enemies can be hit.
Step #10 — To add a raycast we need to have a collider object. For this, 1) Select the ToonSoldier_demo_1 (not the Soldier (1) object which is its controller) and in Inspector; 2) Add Component and 3) choose Capsule Collider; 4) Adjust data like this: Y=1.32; Radius=0.44 and Height=1.63, 5) so that the capsule covers the enemy’s torso (this is one way where you can make your game for professionals players only 🏅);
Step #11 — Let’s tagged the enemies; 1) Select ToonSoldier and in the Inspector, look for Tag, 2) Add Tag…, type enemy and 3) back select ToonSoldier and 4) check enemy as tag for it; in the script, we will use enemy tag to reference this object; Repeat this to the other ToonSoldier;
Now let’s copy this to the other Soldier(2); Do it: Select ToonSoldier_demo_1 click to apply button on Inspector; now select ToonSoldier_demo_2 and click apply again; All settings are copied to the other soldier. This is Prefabs! Cool, right?
Step #12 — Now let’s add the logic to hit the enemies; Go to Crosshair.Update() and here is the complete method:
// Update is called once per framevoid Update(){ this.transform.position = Input.mousePosition; if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { //Debug.Log(hit.transform.gameObject.tag); if (hit.transform.gameObject.tag == "Enemy") { //Destroy(hit.transform.gameObject); hit.transform.parent.GetComponent<Soldier>().Hit(); } } }}
Step #13 — Go to Soldier.cs and create these methods; this will be called when we hit the enemy with the crosshair;
internal void Hit()
{
mAnimator.SetTrigger("fall"); Invoke("MoveRightwards", 1.5f);
}
Good! Now when we hit an enemy the animation fall will be called and 1.5 seconds later MoveRightwards() brings the enemy’s life back…and…
That’s enough for today’s post…we accomplished a key task as we prepared the infrastructure to actually run the fall animation.
In the next post, we will bring Blender to make a custom anime!
Bye-bye for now, thanks, give me your feedback;)
Thanks for your visit! Stay tuned!