- Home /
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:
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
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
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.