• 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 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 this 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
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
2
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
Add comment · 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
1

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
Add comment · 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

What's with this absurd index array input? 1 Answer

Dotted line bouncing on Unity 0 Answers

Mathf.Sin only returning positive values? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges