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 Codelyy · Jul 29, 2016 at 02:43 AM · c#mathmathftrigonometry

Help Understanding Script

I'm sorry if this isn't a type of question I can ask here but I was wondering if someone would be able to help me understand a script that I rewrote in Unity which was originally written in Lua.

This Script:

 public class Bla : MonoBehaviour {
 
     public int spawnTimer = 0;
     private GameObject[] bullets;
     public float sineinc = 0;
     private int speedchange = 20;
 
     public GameObject projectile;
 
     // Update is called once per frame
     void Update () {
         spawnTimer += 1;
             sineinc = sineinc + 0.005f * Mathf.PI;

         if(spawnTimer % 4 == 0)
         {
             var numBullets = 10;
             for(int i = 1; i < numBullets+1; i++)
             {
                 GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
                 int speed = 3;
                 float angle = (((2*Mathf.PI)/numBullets) * i) + (speedchange * Mathf.Cos(sineinc / 1.75f));
                 bullet.GetComponent<Rigidbody2D>().velocity = new Vector2(speed * Mathf.Cos(angle), speed * Mathf.Sin(angle));
                 //bullets.SetValue(bullet, i);
             }
         }
     }
 }

I understand what it does, it does this: alt text

Creating multiple objects that spin around a point. The thing I don't really understand is the Maths... well some of it at least.

One thing I don't understand for example is this line of code: sineinc = sineinc + 0.005f * Mathf.PI; Sineinc and speedchange seem to be used to cause a change in angle so it spins but I don't understand why 0.005f is being times by Mathf.PI since it just seems to cause the spinning to get faster. (It's simple things like that which i don't understand XD)

I also don't really understand how MathF.Cos and MathF.Sin is used in this situation. I have been learning a lot more about trigonometry but I still don't see how it can really be used in these types of situations

Like I said, it's mainly the maths I don't understand that well so I was wondering if someone would possibly be able to explain it? I understand it's quite a lot, sorry about that. I have been changing a lot of the values used to see what the changes do but It's why it works that I want to know.

Thank you

stupid-images-2.jpg (50.9 kB)
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 Trevdevs · Jul 30, 2016 at 04:22 AM

I'm pretty bad at explaining things but I'll do my best.

Mathf.PI is commonly used for the radius, circumference, or area of a circle.pie is an amazing number as well as an amazing dessert ;)

sineinc = sineinc + 0.005f * Mathf.PI is the line of code that gives it that circular angle whatever you wanna call it effect, since sineinc is setting itself equal to itself its updating its old position for the next object that goes out further to be created. I might be wrong here but im pretty sure 0.0005f is the radius of the circle being multiplied by pie to return a circumference in correlation with the objects rotation. I would draw you a picture but i'm lazy sadely

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
0

Answer by kalacia · Jul 29, 2016 at 06:02 PM

I think maybe google would be your friend here. can see that sine is mentioned in the code, so looking into geometry would be good for you. The link below may be a little basic. But its a starting point.

https://www.mathsisfun.com/sine-cosine-tangent.html

It also mentions PI. So i would suggest looking into the maths of circular geomertry. Looking into radius, PI circumference etc.

https://www.mathsisfun.com/geometry/circle-area.html

Again, those links may be simple too you. Or they may be the jumping off point (hehe) you need.

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 Codelyy · Jul 30, 2016 at 02:21 AM 0
Share

I've read through those already

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

198 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

Related Questions

Given a vector, how do i generate a random perpendicular vector? 5 Answers

Distribute terrain in zones 3 Answers

Mapping dot product to positive range 1 Answer

How to calculate inner vertices of a line renderer? (math question) 1 Answer

How can I have a distance between 2 points = 0-1 (min = 0, max = 1) and in between is a decimal? [C#] 2 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