Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SteenPetersen · Aug 06, 2018 at 09:22 AM · directionanglevector2

Projectile Directionality in an isometric view (2D, vector2)

Hi,

I am making an isometric game and I have come a long way. I am actually now making my boss fights and Although I have had some success I find that I am still not fully understanding how to determine directionality of a vector2 and how to control it properly.

here is an example ability of a boss in my game which works fine:

 void FourDirectionalShot(Vector3 initialDirection, int amountOfDirection)
     {
         for (int i = 1; i <= amountOfDirection; i++)
         {
             if (i != 1)
             {
                 initialDirection = Vector2.Perpendicular(initialDirection);
             }
 
 GameObject projectile = PooledProjectilesController.instance.GetEnemyProjectile(gameObject.name, aoeProjectile);
 
 projectile.GetComponent<enemy_Projectile>().MakeProjectileReady();
 projectile.transform.position = projectileLaunchPoint.transform.position;
 projectile.GetComponent<Rigidbody2D>().AddForce(initialDirection * projectileSpeed);
 SoundManager.instance.PlayCombatSound(gameObject.name + "_shot");
         }
     }


This ability shoots projectiles in a maximum of 4 directions as every angle after the first one is simply perpendicular to the previous one.

My question is. What would be a smart way to about shooting in 8 different directions for example, or more. How could I for example get a direction that was in between two of the projectiles in previoiusly mentioned ability. I just want to understand how to better play with this and make some more interesting events.

Any help is greatly appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SteenPetersen · Aug 09, 2018 at 10:26 AM

Inspired by madks, I took a dive into effective ways to go about this for my game and I came up with a simple and very effective solution. An extension Method.

   public static Vector2 Rotate(this Vector2 vector, float degrees)
   {
        float sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
        float cos = Mathf.Cos(degrees * Mathf.Deg2Rad);
          
        float vectorX = vector.x;
        float vectorY = vector.y;
 
        vector.x = (cos * vectorX) - (sin * vectorY);
        vector.y = (sin * vectorX) + (cos * vectorY);
 
        return vector;
 }


This way if I for example wish to fire 12 particles in different yet symmetrical directions I can simply write:

 // first fire a projectile at player with a direction "dir"
 
 for (int i = 1; i <= 11; i++)
     {
         // Given that "dir" is the direction to the player
         Vector2 direction = dir.Rotate(30f * i);

         // fire in the direction above
     }


So a very simple way of doing it and has made my life a lot easier.

Thought I'd share

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by madks13 · Aug 06, 2018 at 11:28 AM

Umm, since your projectiles are GameObjects, you can use rotations instead to turn the objects towards the desired direction. That way you can just add/sub angles.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Converting direction to Vector2 2 Answers

Weird angle returned 1 Answer

How to add slight angle/rotation variation to transform.forward? 5 Answers

Finding the direction an object is moving and giving it a destination 1 Answer

Set rotation based on 2 points problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges