• 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
Question by Major_Lag · Nov 12, 2019 at 05:59 AM · scripting problemmathvector2trigonometry

How can I launch a ball given an angle in degrees and a magnitude?

I'm making a little bullet hell game for fun. Here is what is supposed to happen: I launch a big ball and it goes on for n seconds until it explodes into many smaller balls. These smaller balls are launched into different directions based on the angle intervals I want in the for loop. I was wondering how can I take a degree angle and a magnitude and turn it into a Vector2 so I can change the velocity of each.

So far I have t$$anonymous$$s code:

 void Explode()
     {
         Vector2 spawnLoc = rigidbody2d.position;
         Debug.Log("Exploded");
         for (int i = 0; i < 360; i += 45) //spawn 8 balls and launch in 45deg intervals
         {
             Vector2 direction = degToVec2(i);
             GameObject smallBall = Instantiate(ball, spawnLoc, Quaternion.identity);
             smallBall.GetComponent<Rigidbody2D>().velocity = direction;
         }
         Destroy(gameObject);
     }

and the function:

 Vector2 degToVec2(int degree)
     {
         float x = Mathf.Cos(degree);
         float y = Mathf.Sin(degree);
         Vector2 vector = (new Vector2(x,y).normalized)*speed;
         Debug.Log(vector);
         return vector;
     }

I almost got it right but its not exactly what I want because the angle of 1 or 2 balls launched goes above 360 degrees. I'm pretty sure its because of the way i'm trying to find x and y.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by BastianUrbach · Nov 12, 2019 at 08:33 AM

Mathf.Sin and Mathf.Cos use radians, not degrees. You should multiply degree with Mathf.Deg2Rad. Also, the .normalized is unnecessary, vector already has a length of 1.

Comment
lgarczyn
Major_Lag

People who like this

2 Show 1 · 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 Major_Lag · Nov 12, 2019 at 06:58 PM 0
Share

Silly me, for some reason using radians didn't even cross my mind. Thank you.

avatar image

Answer by Captain_Pineapple · Nov 12, 2019 at 08:48 AM

Hey there,

in general your mistake is common one: you did not read the documentation ;) Mathf.Cos does not take degree but radians as input. so multiply by 3.141/180.0 and you are good to go.


a way to make your code a bit more smooth:

    void Explode(int count)
     {
         Vector2 spawnLoc = rigidbody2d.position;
         Debug.Log("Exploded into " + count + " parts");
         float angleInc = 6.282f / count;
         for (int i = 0; i < count; i++) 
         {
             Vector2 direction = new Vector2(Mathf.Cos(angleInc * i),Mathf.Sin(angleInc * i))*speed;
             GameObject smallBall = Instantiate(ball, spawnLoc, Quaternion.identity);
             smallBall.GetComponent<Rigidbody2D>().velocity = direction;
         }
         Destroy(gameObject);
     }



also you need to read into object pooling. Always instantiating and destroying objects will lead to a performance issue sooner or later.

Comment
Major_Lag

People who like this

1 Show 1 · 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 Major_Lag · Nov 12, 2019 at 07:00 PM 0
Share

Thank you for the responce. You are also right that my problem was I wasn't using radians. I like your code cleanup too, I will consider it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

212 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 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 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

How can I slowly rotate a vector2 to track a target and give velocity based on the angle. 1 Answer

clamp rotation by mouse direction 0 Answers

[RESOLVED] Raycast - Calculate Angular Difference from Object 3 Answers

How to build a car script that allows for car to turn in full circles 1 Answer

Clamp a Vector above minimum angle from another Vector? 0 Answers


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