• entries
4
0
• views
656

## Objective

The main objective of this post is to give a brief introduction to Homing Missiles in Game (2D). A script for Homing missile is also included and explained in the article.

What is a Homing Missile?

What is the logic behind Homing Missiles?

How to use Homing Missiles in Unity?

Have these Questions in Your Mind?

No worries. You will get a satisfactory answer to all those questions, right here.

INTRODUCTION

You might have seen Homing Missiles in movies.

Remembered these scenes from “Marvel’s The Avengers” movie?

If you are not a Marvel fan don’t worry, I am here to help.

Homing Missile is the missile which locks target and chases the target until or unless it reaches the target and blasts itself.

There are plenty of games which are using Homing Missiles.

Excited to implement Homing Missiles in your game?

I know you are.

Let’s set the scene first

Scene Setup:

1. Create two 2D Objects name them “Missile ” and “Target”.
3. Add rigidBody 2D in missile and add Script also name it as ”Homing Missile”

How it actually works?

You might think It is easy to Implement Homing Missile. Just use MoveToward/Lerp until it reaches the target.

Just write code and look at what is going on in your scene.(First try and then read further to understand)

It is reaching the target properly but there is no feel of a rocket. (Right Brain:-What is wrong with my code!)

Below is an actual script for Homing Missiles.

Scripting:

    public class HomingMissile : MonoBehaviour
{
public Transform target;
public Rigidbody2D rigidBody;
public float angleChangingSpeed;
public float movementSpeed;
void FixedUpdate ()
{
Vector2 direction = (Vector2)target.position - rb.position;
direction.Normalize ();
float rotateAmount = Vector3.Cross (direction, transform.up).z;
rigidBody.angularVelocity = -angleChangingSpeed * rotateAmount;
rigidBody.velocity = transform.up * movementSpeed;
}
}

I think script is short but not a sweet one. You can use this homing missile script for your code.

Don’t be scared, I’ll show you how to do it.

• rigidBody -> rigidBody of the missile to add velocity and change angle.
• target -> To get the position of the Target.
• angleChangingSpeed -> The speed to change the angle
• movementSpeed -> Speed of movement

Pure Mathematics: (Open your Brain’s left part)

You need to understand this Picture. That’s How our Homing Missile Works.

First, what you need is “Direction”.

STEP: 1

direction= targetPosition (target.position) - missilePosition(rb.position);

This is how subtraction of two Vector happens (Pythagoras Theorem).

STEP: 2

direction.Normalize();

Normalization means converting vector to unit vector.

Example:

Vector A(3,4)
Normalized(A)=(3/3*3+4*4 , 4/3*3+4*4)=(3/5,4/5)

STEP: 3

Find Amount of rotation to do that.

float rotateAmount=Vector3.Cross(direction,transform.up).z;

Blue Arrow(direction)

Red Arrow(Missile Transform)

green(Resulting Vector).

All three Vectors are perpendicular to each other.that’s why we needed z-axis only.

STEP: 4

Now we need to change angularVelocity of our rigidBody.

rb.angularVelocity = - angleChangingSpeed * rotationAmount.

Minus Sign(-) is used to reverse the direction of movement.

STEP: 5

Adding Velocity to our rigidBody.(just changing the Angle is not Enough).

rb.velocity=tranform.up * movementSpeed

Stop cheering just yet! This is not the end! Missile needs balancing between angleChangingSpeed and movementSpeed.

What if there is no balance between angleChangingSpeed and movementSpeed (Great question! Open right brain is needed here ! )

In the above Image , the missile is heading forward to the target, there is no problem here.

In the above Image, the target has moved to a new position. The missile continues to go toward the target due to angular velocity (in red) , while still going to the place where the target used to be (in black) due to its velocity. (Thinking what is wrong.?)

In the above Image, the missile's velocity forces it to move at the side of the target (black) while the angular velocity tries to pull the missile to the target. (Now you might feel there is something suspicious).

In the above Image, the missile falls into a stable orbit around the target and never reaches its goal.

The black arrows indicate a velocity vector while the red lines indicates angular velocity. (That’s why need a balance between angleChangingSpeed and velocity).

Considering that there is no friction in space, there is nothing to slow the velocity of the missile down and collapse the orbit.

That is how whole Universe is trapped (Thank God for this error else there is no life!).

Now, How to Fix this Problem ?

Well, There is no particular formula to balance them, but by Increasing angle or decreasing velocity will help you there.

### Caution

• Don’t go with very small speed so your missile loses the feel of Homing Missile and Don’t make it Extreme fast it will be trapped in the orbit.
• Don’t make angleChangingSpeed too small else it will fall in to trap and to high angleChangingSpeed loses the feel of your game.

That’s It for Homing Missile. Hoping to see homing Missiles in your game!

Please, do Share our blogs with others if you like our content

Originally published at TheAppGuruz - Mobile Game Development Company on May 31, 2018.

## Different Ways Of Doing Sprite Sheet Animation In Unity

Objective The main objective of this blog post is to give you an idea about how to do Sprite Sheet Animation in Unity3D. Are you looking for animation in your 2D games? Do you want your Character have some real feel and look more living ? Then here it is, I’ve brought you some easy and quick ways to make your game look attractive and give a real life feel to the character of your game. In this post we will learn how to give animation to your game character with 2 easy ways. By Animation And Animator controller. By Scripting. Interesting right..? So tight up your seat belts and get ready for this joyful ride! Let’s start with the first option. 1. By Animation And Animator Controller First set up the scene like the given image.

Don't forget to add sprites folder that will be use for the the different animation to your scene, if you don't have the Sprites you can download it from here.
Create a 2D sprite object and assign the first sprite of the idle Animation to it. (Idle animation is the animation in which the character is just standing still and breathing no other activity).
Create the Script named AnimationWithScripting and attach it to hero Object.
Now, follow the below steps to create the different animation.
First select all the sprites of Idle Animation folder and drag it on our hero object and drop there, a new animation will be created. Then Unity will ask to name the animation name it as Idle.
Then select all the sprites of Kick Animation folder and drag it on our hero objectand drop there , a new animation will be created. Again Unity will ask to name the animation name it as Kick.
Then select all the sprites of Walking Animation folder and drag it on our hero objectand drop there, a new animation will be created. Again Unity will ask to name the animation name it as Walk. I hope you all are ready with your 3 animations.
The animator controller will be automatically added to our hero object.
Now set the transitions and trigger as per the below image.

See what Triggers are to be set in the following transitions: Transition Trigger Idle -> Walking transition Walk Walking -> Idle transition Idle Kick -> Walking transition Walk Walking -> Kick transition Kick Idle -> Kick transition Kick Kick -> Idle transition Idle Let’s see the code is to be written in the Script ScriptToAnimate.cs using UnityEngine; using System.Collections; public class ScriptToAnimate : MonoBehaviour { public Animator animator; void Update() { if (Input.GetKeyDown(KeyCode.I)) { animator.SetTrigger("Idle"); } if (Input.GetKeyDown(KeyCode.K)) { animator.SetTrigger("Kick"); } if (Input.GetKeyDown(KeyCode.W)) { animator.SetTrigger("Walk"); } } } Let’s see what's there in the scripts. Name Type Description Animator animator; variable To store the reference of the Animator Controller of the object Update() Unity Callback To write conditions according which the animation is To be triggered Go to Unity now and play the scene you will see the idle animation playing default. Now press the keys I, K and W respectively and check. Did you see your character..? Yeyy ! your character got life. Your character is breathing, kicking and walking. I'm sure you are on the right track. Now, let’s move to another method. By Scripting First set up the scene like the given image.

Don't forget to add sprites folder that will be use for the the different animation to your scene, if you don't have the Sprites you can download it from here.
Create a 2D sprite object and assign the first sprite of the idle Animation
Create the Script named as AnimationWithScripting and attach it to hero Object and write the following code in it

AnimationWithScripting.cs using UnityEngine; using System.Collections; public class AnimationWithScripting : MonoBehaviour { public SpriteRenderer spriteRenderer; public Sprite[] walk; public Sprite[] idle; public Sprite[] kick; void Start() { StartCoroutine(Idle()); } void Update() { if (Input.GetKeyDown(KeyCode.I)) { StopAllCoroutines(); StartCoroutine(Idle()); } if (Input.GetKeyDown(KeyCode.K)) { StopAllCoroutines(); StartCoroutine(Kick()); } if (Input.GetKeyDown(KeyCode.W)) { StopAllCoroutines(); StartCoroutine(Walk()); } } IEnumerator Idle() { int i; i = 0; while (i < idle.Length) { spriteRenderer.sprite = idle[i]; i++; yield return new WaitForSeconds(0.07f); yield return 0; } StartCoroutine(Idle()); } IEnumerator Walk() { int i; i = 0; while (i < walk.Length) { spriteRenderer.sprite = walk[i]; i++; yield return new WaitForSeconds(0.07f); yield return 0; } StartCoroutine(Walk()); } IEnumerator Kick() { int i; i = 0; while (i < kick.Length) { spriteRenderer.sprite = kick[i]; i++; yield return new WaitForSeconds(0.07f); yield return 0; } StartCoroutine(Kick()); } } Now go to Unity and add the sprites of walking, kicking and idle state in their following lists in the inspector as per the image.

Let’s see what's there in the script. But wait... In this script, the coroutine is used for Sprite Sheet animation. If you don’t know how to use coroutine. Don't worry our coroutine unity blog post will help you. Trust me you will be master in 10mins. Name Type Description Start() Unity Callback To start the idle coroutine. SpriteRenderer spriteRenderer; variable To store the reference of the spriterenderer of the object. Sprite[] walk; list To Store walking Sprites Sprite[] idle; list To store idle Sprites Sprite[] kick; variable To store Kicking Sprites Update() Unity Callback To write conditions according which the animation is played IEnumerator Idle() coroutine To Run the Idle Animation IEnumerator Kick() coroutine To Run the Kicking Animation IEnumerator Walk() coroutine To Run the Walking Animation Now, go to Unity and play the scene you will see the Idle animation playing default. Now press the keys I,K and W respectively and check. Yeahhhhh ! Again you see character, its breathing kicking and walking. But wait which one is more optimised? Yes, maybe you would be having the same question. Both the methods run the same way when checked in the profiler. So, it's up to you whichever you feel easy and quick and in accordance to the requirement of your game. Feel free to contact us if you really liked this blog. And don’t forget to share your comments below!