← Back to all addons

8 Directional Projectiles (That's right)

Posted by 1MGD on Aug 27, 2024 at 3:20 AM

GameplayExperimentalFeature
🔥 7 💛 1

I saw some people asking about 8 directional projectile attacks for the player and enemies. It’s not as smooth as I’m sure OUR GLORIOUS CREATOR could make but this actually made my game a lot more fun and is easy to implement.

Here’s how to attack in the direction you or the enemy is moving in for more immersive enemies (make a backup first):

Open up CharacterBase.cs and insert this code with the properties up top. Screenshot of exactly where I put mine + the full file attached in case this makes no sense.

 public Vector2 FacingDirection
{
    get
    {
        if (movementDirection != Vector2.zero)
        {
            return movementDirection.normalized;
        }
        else
        {
            return GetLookAtDirection() == EDirection.Left ? Vector2.left : Vector2.right;
        }
    }
}

Then Open up ProjectileAbility.cs and replace your ThrowProjectile() method with this. Again, Screenshot of where I put mine + the full file attached in case this makes no sense:

private void ThrowProjectile(int projectileIndex)
{
    GameObject projectileInstance = m_projectilePool.GetAvailableInstance();
    projectileInstance.transform.position = m_projectileSpawnPoint.position;
    projectileInstance.SetActive(true);
    Projectile projectile = projectileInstance.GetComponent<Projectile>();

    Vector2 baseDirection = m_character.FacingDirection; // Get the character's current facing direction.

    float angleOffset = (m_sheet.spread / m_sheet.projectileCount) * projectileIndex - (m_sheet.spread / 2.0f);

    // Rotate the direction by the spread angle.
    Vector2 direction = Quaternion.Euler(0, 0, angleOffset) * baseDirection;

    projectile.Throw(DamageSolver.SolveDamageOutput(m_character, 
m_sheet.damageDescriptor), direction, m_sheet.projectileSpeed);
}

Let’s cook!!

💬 Comments (8)

Vaughn Aug 27, 2024 at 05:16 AM
This is amazing
🤠 1
Gyvr Aug 27, 2024 at 02:51 PM
Very nice! Great job 😄 I’d suggest adding your code blocks in between 3 ` so it shows up as a block instead of a line
Gyvr Aug 27, 2024 at 02:52 PM
1MGD Aug 27, 2024 at 02:55 PM
Nice! Didn’t know about that little trick 😎
Gyvr Aug 27, 2024 at 02:56 PM
Even better, you can use this to enable syntax highlighting:
1MGD Aug 27, 2024 at 02:58 PM
What is this sorcery?!
😄 1
Gyvr Aug 27, 2024 at 02:59 PM

And of course you can replace “csharp” by any language:

  • python
  • cpp
  • c
  • java
  • etc…
1MGD Aug 27, 2024 at 03:04 PM
Now that’s beautiful. I just got a tutorial making my tutorials.
DogeCool 1 🎉 1

Want to continue the conversation?