Skip to content

Instantly share code, notes, and snippets.

@curious-username
Last active November 23, 2021 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curious-username/73a14a7d3c8495476a00d4f53f035c69 to your computer and use it in GitHub Desktop.
Save curious-username/73a14a7d3c8495476a00d4f53f035c69 to your computer and use it in GitHub Desktop.
planning missile for the enemy object
CREATE A HEAT-SEEKING MISSLE
GRAPHICS
---------
SMALL RED MISSLE
RESIZED TO ENEMY AND PLAYER TRANSFORM.SCALE
VFX
---------
EXPLOSION ON COLLIDER CONTACT
SOUND
---------
EXPLOSION SOUND
SCRIPTING
----------
MISSLE_ENEMY.CS - Will instantiate the missle object
ENEMY_MISSLE.CS - Missle fired FROM the enemy and the behaviors associated with the missle.
STEPS FOR SCRIPTING MISSLE_ENEMY.CS
---------------------
- Serialize a GameObject object variable to assign a prefab
- On spawn, the missle will fire
STEPS FOR SCRIPTING ENEMYMISSLE.CS
---------------------
WHY WAS A SEPERATE SCRIPT MADE?
The ENEMY will shoot off the missle. The missle needs to know how to get to the player. Not the enemy.
HOW IS THE OBJECT CREATED?
INSTANTIATE the prefab through the missle_enemy script
WHAT do I need to have for the missle to move in the player direction
a GameObject to store the player location
Start()
needs to find the player
Update()
Modify the missle transform using translate. Will utilize Vector2.Down * _speed * Time.deltaTime
Create a Vector3 direction to store the player transform.position and subtract it's own transform.position
Now the missle knows how to move down and the direction of the player. Need rotation to give the effect of a heat seeking missle.
Found this super neat formula to calculate rotation of the missle based on the player location:
float rot_z = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rot_z + 90);
This handles the missle going in a downward direction at start, after initial spawn, the missle will adjust.
If the player fired the missle it would be a -90 adjustment of rot_z.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment