- Home /
How to make a Homing Attack like in Sonic the HedgeHog?
Hi! I am making a Sonic game, and I want to make a Homing Attack special ability. Could anyone help me?
Description of Homing Attack: when Sonic is jumping (or descending) and if any enemies or objects are close Sonic can aim at them and fly in their direction. After striking an enemy or object, he is knocked back up into the air and can do another Homing Attack. If there are no enemies and objects around Sonic, he can only do a Jump Dash forward. I hope, somebody will understand my not-so-good English and help me...
I fixed your English. It wasn't that bad. There were only one or two mistakes. :)
Answer by skovacs1 · Aug 24, 2010 at 05:16 AM
This is not a good question - too unspecific and open for debate. People are not here to write your game for you.
One way to do it:
- Tag your homing targets appropriately
- In a script somewhere, store a public array of Transforms.
- Have a sphere or other collider attached to your character. Set it as a trigger.
- To this collider object, attach a script with OnTriggerEnter which adds the target's transform to the public array, checking their tag and OnTriggerExit which adds the target's transform to the array, checking their tag.
- In the spawn scripts that create targets, check if they are inside the trigger and them to the array.
- If the target destruction scripts doesn't destroy() them, be sure that they are removed from the array.
- In the script with the array, in an Update function check if the character is in the air and if the homing attack button is pressed and if the array is nonempty, set the character's target to be the transform position of the nearest target in the array and set a flag to jump attack (you can store specific jump attack target transforms if you like).
- In the script that moves the character, check the flag for the jump attack and if it is set, move the character towards the target in stead of normal movement controls.
- In the a script on the character, include in the OnCollision a check for a homing attack flag. If the flag is set, unset the flag, perform damage calculation and perform a jump the same as you would on the ground.
In stead of using a trigger area and a public list, you could instead check against every single enemy's transform.position and check the distance squared to your character's position. If they are within a certain range, add them to a list local to the Update function.
In stead of the OnCollision to do the damage and jump, you could alternatively check if the positions are equal.