How to Simulate Gravity in Unity

Поделиться
HTML-код
  • Опубликовано: 20 дек 2024

Комментарии • 591

  • @bip901
    @bip901 5 лет назад +370

    *IMPORTANT NOTE*
    When using the .magnitude property of a Vector3, Unity performs a costly square root calculation.
    A much quicker option would be to use .sqrMagnitude to get the magnitude squared, which is what you need in the formula anyway.
    Currently you're taking the square root of something and then squaring it, which is just wasted calculations.

    • @creepercloud2617
      @creepercloud2617 5 лет назад +3

      when i replaced magnitude with SqrtMagnitude the gravity started acting alot slower, so your wrong

    • @bip901
      @bip901 5 лет назад +73

      @@creepercloud2617 You've probably forgotten taking away the squaring [At 3:40, Math.Pow(distance, 2) should be replaced by simply distance, because now it's already squared].

    • @creepercloud2617
      @creepercloud2617 5 лет назад +27

      yay it works thank you

    • @bip901
      @bip901 5 лет назад +22

      @@creepercloud2617 You're welcome!

    • @CosyScripter
      @CosyScripter 4 года назад +9

      @@creepercloud2617 Lol dont say that before you know

  • @whoboy5763
    @whoboy5763 4 года назад +181

    its sad to see him go but the videos will live on

  • @miloalbers9564
    @miloalbers9564 7 лет назад +247

    Super useful, I was about to purchase a $30 gravitations pack from the asset store! Thanks for saving me the money, I might donate it to you instead :)

  • @jonathansmith114
    @jonathansmith114 7 лет назад +158

    We put an Attractor in your Attractor so you can attract Attractors while attracting etc...

  • @LideCantFly
    @LideCantFly 7 лет назад +8

    I use Unity since a year and a half. I was a trainee in a company for software development and currently, I study Applied computer science. Still, I watch every single video of you and learning so much. Thank you for all your great videos. Awesome!

  • @uRealReels
    @uRealReels 4 года назад +1

    Super Gravity video -i learned so much from this video about Gravity simulation.... 1] normalized/direction now makes sense to me.. it quickly lets u set direction like lookat. 2] attractor is not a fancy Unity word, he just created function that can be called. 2.71828... ] rb.mass can be used to access the mass value of rb 4] Findobjects of type lets u scan for things to attract
    just amazing what i learned from this video - great job, dude!
    PS; did u know that indiana once proclaimed by law that PI was about 4.

  • @michealvincent731
    @michealvincent731 5 лет назад +16

    Still relevant in 2019, awesome overview on how to approach it.

    • @spacevolvox4020
      @spacevolvox4020 4 года назад

      2020

    • @TheRoboticLlama
      @TheRoboticLlama 4 года назад +4

      I hear that isaac newton plans to change his famouss gravitational equation in early 2021- so thankful I saw this now for my game!

    • @terrsus
      @terrsus 4 года назад

      @@spacevolvox4020 He commented this a year ago

    • @spacevolvox4020
      @spacevolvox4020 4 года назад

      Thomas de Langen ya ik just pointing out how it is still relevant in 2020 although i could’ve been more descriptive

    • @terrsus
      @terrsus 4 года назад

      @@spacevolvox4020 ah k lol

  • @happybadger2270
    @happybadger2270 4 года назад +4

    If i had known in 10th grade you could do lots of cool stuff with mathematical equations I would've paid attention. No seriously, unity should be used as an educational ressource at school. I mean its the perfect tool to learn programming, apply physical knowledge and see mathematical equations and logic applied to a specific problem. This is awesome.

  • @olivialeigh1244
    @olivialeigh1244 7 лет назад +1

    I love this channel. I am new to programming and find most tutorials boring but these videos are perfectly explained, timed, and even have some comedic effects to them. So in short thank you.

  • @nuclear7622
    @nuclear7622 7 лет назад +73

    4:36 so many tractors...

  • @abdulkadirdursn
    @abdulkadirdursn 3 года назад

    September of 2021. still best unity tutorials on youtube. Thank you.

  • @chronofactor2037
    @chronofactor2037 3 года назад +4

    I'm currently making a particle physics game and it worked for 2d too! I am soo happy with how it is currently turning out!

  • @es330td
    @es330td 2 года назад +1

    Thank you so much for posting this. I'm mucking around with gravitational attraction between objects. This is going to save me a lot of work.

  • @loadingninjavods4648
    @loadingninjavods4648 5 лет назад +1

    I have so much fun with this, i made an object that instantiates different planet sizes every few seconds in a given are and in a given amount and playing with this is just so much fun.
    Thank you very much for making my evening!

  • @theebulll
    @theebulll 6 лет назад +1

    I love how to-the-point and well explained this is. 9 minutes of material that would normally take another person 45 minutes to trudge through.

  • @Dmittry
    @Dmittry 2 года назад +6

    And that's how Brackeys created the Universe. In the beginning was the Word. And the Word was 'internal class Creator'.

  • @kurekureci
    @kurekureci 7 лет назад +352

    "How to create Universal Sandbox under 10 minutes"

    • @generalkitten2100
      @generalkitten2100 7 лет назад +1

      vistaero well, real numbers and explosions. Good way to give objects orbit propably would be handy too.

    • @justaprogrammer6031
      @justaprogrammer6031 5 лет назад

      @@vistaero herd of a joke in your life?

    • @riverfilms4548
      @riverfilms4548 4 года назад

      I ask myself if it would be possible to get planets in their orbits with just the gravity. :O

    • @benzenatizineeddine7816
      @benzenatizineeddine7816 4 года назад

      if he used point effector then it takes only 3 minutes

    • @Ryan-xq3kl
      @Ryan-xq3kl 4 года назад

      @@riverfilms4548 Probably not just one gravity parameter but once you scale them appropriately with mass according and get a whole bunch of experiments with it done you could definitely have an orbit simulation.

  • @jeezlouix
    @jeezlouix 5 лет назад +1

    Please make an updated tutorial of this but using ecs and dots 🙏🙏 I would love to see how you tackle that!

  • @PerfectlyNormalBeast
    @PerfectlyNormalBeast 4 года назад

    8:40 Never use == with float and double. Use Mathf.Approx (or whatever it's called). Also, since F=1/D^2, the force gets large fast as D approaches 0. So either clamp max force to something reasonable, or if D

    • @PerfectlyNormalBeast
      @PerfectlyNormalBeast 4 года назад

      Relying on the collider to keep distance reasonable is living dangerously
      So is storing objects in a naked static variable

  • @chetmcgovern9985
    @chetmcgovern9985 6 лет назад +4

    Great video!
    If I had 5 dollars for every time you said attractor, I wouldn't need to go to work anymore.

  • @levil3409
    @levil3409 6 лет назад +5

    You could have used direction.sqrMagnitude to avoid a square-root call and immediately get the distance squared. It would also be better to use a simple multiplication when you are just squaring, because Mathf.Pow() is slower. Finally, the problem is not directly that the distance is 0, but that normalizing a vector with 0 length causes a division by zero.
    However, this is a nice tutorial, and I especially liked how you avoided FindObjectsOfType().

  • @AllieM.J
    @AllieM.J 6 лет назад +4

    Bit of a an old video now, but it got me thinking of some interesting ideas for a game project I'm working on in unity. I'm wanting to create a procedural voxel planet, also known as marching cubes. Where it obits a sun in a 3d space, a tutorial on how I could achieve this would be greatly appreciated. I have researched online for resources but either no one wants to share said knowledge, or has said knowledge but would rather charge me a fortune for it. I've watched your video's for a good while now, and seeing this particular one gave me some possible hope that you might be the outlet to figure this solution out.

    • @itsaducklin
      @itsaducklin 2 года назад

      Sorry for saying this 4 years later but if you want orbit, you could just make a public vector3 and call it initialVelocity, and then set it to something in the inspector. Then in the start method, just set your rb velocity to initialVelocity. Experiment with the initialVelocity, and you should have proper orbit :>

  • @gorkemvids4839
    @gorkemvids4839 5 лет назад +1

    This could be optimized even further. Once you calculated a force from an object, exact same force will aply to other object you put in to equation. So you can half the calculations. This can be done from a single script with these codes.
    list bodies;
    foreach(Rigidbody r in bodies
    {(foreach(Rigidbody b in bodies)
    {
    force=calculation(r,b);
    a.addforce(a);
    b.addforce(b);
    }
    }

    • @megazilla344
      @megazilla344 5 лет назад

      I went and wrote that code.
      First, change your fixed update.
      void FixedUpdate()
      {
      int thisIndex = Attractors.IndexOf(this);
      int listSize = Attractors.Count;
      for (int i = thisIndex + 1; i < listSize; i++)
      {
      Attract(Attractors[i]);
      }
      }
      And throw this at the end of your attract method
      rb.AddForce(-force)

  • @Xerobound
    @Xerobound 7 лет назад

    Using Random.InsideUnitSphere you can create some awesome effects, spawning random amounts of objects around a "sun" at variable distances, and adding a random amount of torque. You can also tell the object to look at the central spawn object (just for the start, before the torque is applied), and apply velocity left or right, to help create an orbit effect.

  • @LegendaryWizardPS
    @LegendaryWizardPS 7 лет назад +54

    I'm 11 years old, looking to be an unprofessional indie game dev, the math part can sometimes get confusing for me since I'm only starting 6th grade, so that's where RUclipsrs like Brackeys kick in :P

    • @julianpizano5586
      @julianpizano5586 6 лет назад +8

      Sagacious once you have an understanding of basic algebra it'll be a lot easier

    • @moeina960
      @moeina960 5 лет назад +2

      sagacious if you want to learn it you can take a look at the khan academy website where you can learn algebra from the firs .i think you can understand it easily.
      "link"= www.khanacademy.org/

    • @zeroiq4737
      @zeroiq4737 5 лет назад +2

      What did you learn in this 2 years?

    • @joelwebber1798
      @joelwebber1798 4 года назад +1

      @@zeroiq4737 If he's thirteen now then he should have started algebra XD

    • @dominusnova5204
      @dominusnova5204 4 года назад

      @Joel Webber he’s probably acing it

  • @TheOriginalDarkGlitch
    @TheOriginalDarkGlitch 7 лет назад

    This Tutorial was awesome! You just gave me a huge inspiriation, thanks!

  • @bdotsamir
    @bdotsamir 4 года назад +9

    drinking game every time he says “attractor” take a shot

    • @sa_ad
      @sa_ad 4 года назад

      Better version eat a ghost pepper everytime he says "attractor"

  • @davannaleah
    @davannaleah 3 года назад +2

    Just a suggestion. I thought a more efficient way would be, for each planet, to sum the forces from each of the others then apply the sum to itself.
    See the code snippet below:
    void FixedUpdate()
    {
    Vector3 forceVector = Vector3.zero;
    foreach (GravitatingObject otherPlanet in _planets)
    {
    if (otherPlanet == this)
    continue; // don't include ourselves!
    Vector3 direction = otherPlanet.transform.position - transform.position;
    float distance = direction.magnitude;
    float forceMagnitude = (G * ourRigidBody.mass * otherPlanet.GetComponent().mass) / (distance * distance);
    direction.Normalize();
    // sum the forces from the individual planets
    forceVector += direction * forceMagnitude;
    }
    // apply the resulting force to us
    ourRigidBody.AddForce(forceVector);
    }

  • @maximorodriguez4831
    @maximorodriguez4831 7 лет назад +1

    I am just now rediscovering your videos, and so far having a blast, really like the way they are made and structured, already subscribed. Not sure if it was mentioned already, but will throw my two cents here, when working with floating point, I find it best not to check against an exact value (i.e. distance == 0f) but to work with thresholds or a minimum epsilon value that will work. Meaning instead of checking if the distance is exactly equal to zero (which could bring trouble with floating point) the condition could be for the distance to be less than or equal than a certain tolerance constant (as small as you want it to be). Keep up the amazing work :)

  • @ChristianKroken
    @ChristianKroken 7 лет назад

    Great tutorial! Visual Studio 2017 has a handy unity shortcut Ctrl+Shift+Q to generate Monobehaviour callback functions by the way.

  • @TheAntimon13
    @TheAntimon13 6 лет назад

    Again such a cool tutorial. Where would I be without Brackeys!

  • @supermig2582
    @supermig2582 4 года назад

    Brackets ur the best mange tak for alt den hjælp:) jeg har været fan i 2 år nu.

  • @atomicatom4187
    @atomicatom4187 6 лет назад

    You can refer to storing all objects in a static collection which register themselves when they enable/disable as the "Observer Pattern".
    - Entities store themselves in a specific global collection.
    - Everything else "observes" this collection to manipulate and/or interact with the entities inside of it.
    - You don't need a list, a static self expanding array may be cheaper.
    This is a pattern I use for pretty much everything, as GameObject.Find and GameObject.FindObjectsOfType should be avoided to prevent bad practices outside of Start and Awake.

  • @mazander_man
    @mazander_man 7 лет назад +5

    Hey, Brackeys, I really have enjoyed your channel and I would like to thank you for the effort you put in each video. I just wanted to suggest a few topics that possibly you could go over in future videos. First, I would like to know if there could be a way for several people that watch this channel to use Unity collaborate and make a big project. Secondly, I would really like it if, after this RPG series, you do another survival series but using blender and going a bit more in depth with more concepts. Thanks!

  • @blaptrap
    @blaptrap 7 лет назад

    Your videos are the best, thank you so much for them. As soon as I'm able to make some money from my games I'll be sure to pass along your cut. ^^

  • @mustafashakeel4621
    @mustafashakeel4621 7 лет назад +1

    Niceee video ;) best unity tech RUclipsr

  • @executeinc9756
    @executeinc9756 3 года назад +4

    the Attractor in the
    "void Attract (Attractor objtoattract)"
    shows as an error and won't let the game run, does anyone know a solution?

  • @onezerodev1756
    @onezerodev1756 7 лет назад

    My favorite Chanel . Thanks for this tutorial

  • @greatmallard9318
    @greatmallard9318 7 лет назад +4

    You should issue a challenge to all your viewers to make a small video game using only your tutorials

  • @goldcobraarima9819
    @goldcobraarima9819 6 лет назад

    You make the best videos man!

  • @hjc1710
    @hjc1710 4 года назад

    Great video as always man, you're the best!

  • @EyTcTh
    @EyTcTh 7 лет назад

    I love your videos so much because they help me a lot with my games :D

  • @BingXhz
    @BingXhz 7 лет назад +1

    Thank You For The Best Understandable Tutorials!

  • @mip7513
    @mip7513 4 года назад +1

    I used Mathf.Pow(DistanceBetweenObjects, 2) to calculate square magnitude between two objects, but I found out, that they are very handy thing "Vector3.sqrMagnitude", that saves one line of code each time you calculate square distance - you need just put it into the formula!

  • @kyleme9697
    @kyleme9697 4 года назад

    Neat, love it, thanks again !! So many crazy weapon ideas now muuuaaaaahhhhhhhh.

  • @hamedazaddoulat6676
    @hamedazaddoulat6676 7 лет назад

    Man , you are an awesome teacher , thanks so much !

  • @genervterNutzer
    @genervterNutzer 6 лет назад

    Very similar approach like my one. To improve performance, there's no reason to get the square root of `sqrMagnitude` (which `magnitude` in fact does) and calculate the power by 2 (square) again. Further more I instanciate another `MonoBehaviour` derived class at runtime which triggers in `FixedUpdate` my static method for calculation of all object pairs at once. On adding objects to list, I generate a new list of pairs, so the same calculation works for both paired objects with just switching to the negative normalized vector.

  • @Yolwoocle
    @Yolwoocle 4 года назад

    Dang, I'm already feeling like I can make an entire universe simulator haha. Great tutorial!

  • @desmondas3043
    @desmondas3043 7 лет назад +3

    Does anybody know an equally-amazing channel, but for Unreal Engine?
    btw. I watch your every video, even though I use UE and not Unity. I just love this channel :)

  • @lukeystuff
    @lukeystuff 4 года назад

    Thank you so much! I was looking for a tutorial like this one to help me with my game!

  • @LupusZockt
    @LupusZockt 3 года назад

    Thank you! That helps me a lot with a current project!

  • @amwcodes9715
    @amwcodes9715 7 лет назад

    This was great. I'd love to see it expanded to create a solar system with orbital patterns etc..

  • @Seeter1000
    @Seeter1000 3 года назад +2

    imagine calling your triplets sara, sarah ans sahra. thats how this code is written

  • @starman8853
    @starman8853 6 лет назад +3

    Does anyone know how I can calculate a trajectory with this? I was thinking of an invisible game object with a trail that goes ahead but I am not sure how I could make it go ahead almost instantly without getting shot out of orbit at an escape velocity or just shot into the main body

    • @StrawberryXetr0n
      @StrawberryXetr0n 3 года назад +2

      this probably won't be interesting anymore after 2 years but you can take the momentum of the rigid body (after the forces have been applied) and normalize that. that's the direction your rigidbody is heading

    • @starman8853
      @starman8853 3 года назад

      @@StrawberryXetr0n Lol yeah I'm well past whatever I was messing around with this for but still, thanks

  • @N0biKn0bi
    @N0biKn0bi 6 лет назад

    One of your best Videos in my orion. Ehm opinion!

  • @loganduncan4241
    @loganduncan4241 2 года назад +1

    I think I need help. I have done everything exactly as you said so far (at least if im not missing something) but Im getting complier errors and cannot enter playmode.

  • @mikkachu7824
    @mikkachu7824 3 года назад +1

    i keep getting an error message telling me "the type or namespace 'Attractor' could not be found (are you missing a using directive or an assembly reference?)" Does anyone know a fix for this? It seems as though unity doesn't recognize Attractor as a valid array? I'm not sure but any help would be great

  • @nepnet9903
    @nepnet9903 7 лет назад +4

    Cool. Can you make a tutorial about simulating magnets?

    • @subramanyam2699
      @subramanyam2699 6 лет назад

      Lame nickname en.m.wikipedia.org/wiki/Force_between_magnets
      There the force between magnets roughly changes by 1/X^4

  • @dolphano2951
    @dolphano2951 3 года назад

    Thank You!!! This literally saved me for a game jam (GMTK 2021)

    • @connormarks5584
      @connormarks5584 3 года назад

      how did you get it to work

    • @dolphano2951
      @dolphano2951 3 года назад

      @@connormarks5584 For my game I wasn't working with planets, so I set the masses of the objects pretty low like 2 kg (try not to make masses 1 or anything below 1). I then wrote the code, but I didn't include the gravitational constant (G) since I wasn't working with planets with big masses 6:10. I also stopped the video at 7 minutes and didn't do the stuff afterwards.
      If none of that works, then feel free to answer these questions in a reply and I could get a better idea of how to fix it!
      What are you trying to do in your project? How far apart are the objects?
      What is going wrong (Is the code not effecting them at all or are they teleporting everywhere)?
      What are the masses of the objects?

    • @connormarks5584
      @connormarks5584 3 года назад

      @@dolphano2951 Ok so I tried everything that you said but nothing seems to change the issue.
      I decided to download the project from the link and there was an issue with the script min drawer I assume it was a post processing thing but it worked after I added this line of code at the top using MinAttribute = UnityEngine.PostProcessing.MinAttribute; but then after I hit play instead of the objects all coming together they where separating its as if the gravity was backwards. The mass of the objects is 1000 even after I changed it they only moved apart slower I have no idea why it will not work

    • @dolphano2951
      @dolphano2951 3 года назад

      @@connormarks5584 oh man that's pretty weird. I'm not too knowledgeable on post processing as of now, but you mentioned the gravity was reversed like repelling. This may sound kind of rookie but did you accidentally place a negative sign somewhere in the code?

    • @connormarks5584
      @connormarks5584 3 года назад

      @@dolphano2951 Well tbh I am a bit of a rookie but as I mentioned I downloaded the project so I didn't write the code

  • @krishnerkar2580
    @krishnerkar2580 5 лет назад

    Is Attractor a type of component or some variable, and how do you know which objects are of type attractor in 4:36

  • @333jordyg
    @333jordyg 7 лет назад +2

    I have a question about this attractors script!
    I'm trying to use it for a large number of objects: 125 cubes. When I do that, the framerate just plummets. What kind of solutions should I look for so I can make a game about gravity with a lot of objects?

  • @Edzmens19
    @Edzmens19 6 лет назад

    Add if statement after distance calculation. Lets say if(distance > 50)do what Brackeys code does, else do the same but AddForce(-force). Create separate G var for both conditions and have fun

  • @fisslewine1222
    @fisslewine1222 7 лет назад

    Loving your tutorials!...and have 3 questions:
    1. how would you get the attractor force to go to 0 after a certain distance has been reached?
    2. how could I assign gravity to the face of a plane or make the inner faces of a tube have gravity?
    3. how would I make a low gravity areas for a first person controller?

    • @jean-naymar602
      @jean-naymar602 7 лет назад

      1) if(distance < myMaxDistance){
      //do the attraction stuff
      }else{
      //do nothing (obviously, you don't need the else here, just to explain)
      }
      2) there's a lot of solution to that... But it's to complex to answer without illustration. I'd recommend you to check unity's forum, you can ask anything there and people will help you, even if you're a complete beginner.
      But really quickly I see 2 eassy way of doing that :
      a) handle gravity on your own, just like in this episode, but you'd have to set the direction of the force to be the opposite of the plane's normal.
      b) just change the unity gravity direction vector somewhere in preferences to, like above, to be the inverse of the plane's normal. You can edit that under Edit>Project Settings> Physic but if you want your plane to change orientation, you'll have to change update it via scripting.
      3) Same location as above Edit>Project Settings> Physic, you can tweak gravity intensity & direction.

    • @fisslewine1222
      @fisslewine1222 7 лет назад

      what I mean is what maths equation would you use to gradually return a force to 0 over time?

    • @fisslewine1222
      @fisslewine1222 7 лет назад

      Ok thanks, one more thing and this may seem to be a stupid question...but is there a way to determine my plane's normal (even when it is a curved object) in unity?

  • @thepithief3700
    @thepithief3700 7 лет назад

    Great video! Very informative !!

  • @ContinuedBread
    @ContinuedBread Год назад

    This is so much more optimized than all my solutions :p

  • @HomeofLawboy
    @HomeofLawboy 7 лет назад

    Very nice video. A optimization that I would suggest is calculating the distance squared (using pythagoras) and plugging it directly into the force formula, because Vector3.magnitude uses square root and is a costly operation.

    • @dannywheeler6497
      @dannywheeler6497 7 лет назад +1

      So use direction.sqrMagnitude

    • @HomeofLawboy
      @HomeofLawboy 7 лет назад

      Wait that's a thing? How could I never have noticed before. Thanks

  • @Michko_
    @Michko_ 7 лет назад +2

    Awesome, just what i was looking for. :)

  • @lukeystuff
    @lukeystuff 3 года назад

    The type or namespace name 'Attractor' could not be found (are you missing a using directive or an assembly reference?)

  • @CyanStudios24
    @CyanStudios24 7 лет назад

    Wonderful video! I knew physics would become useful at some point :D

  • @SovakaKindred
    @SovakaKindred 7 лет назад

    Just add a require component at the start of your script. - [RequireComponent(typeof(Rigidbody))]
    Then you won't need to worry about remembering to add one yourself. You can do the same with colliders

  • @ChumperDumper
    @ChumperDumper 7 лет назад

    Something people sould remember is that realistic Earth gravity, when walking on Earth is: gravity + (objectMass * airResistance). Without air resistance​, every object moves at the same speed downward. This was proving by the us in a chamber completely removed of air. They dropped a bowling ball and a feather at the same time and they fell at the exact same speed.

    • @goji5887
      @goji5887 7 лет назад

      Well... technically speaking we haven't really done _complete_ removal of air yet... That's way too hard due to the immense vacuum you create

    • @jean-naymar602
      @jean-naymar602 7 лет назад +1

      You don't need a vaccum chamber to show that. Just 2 line of math using Newton's equations. So the US didn't prove shit. Newton did.

  • @MindHawkHotS
    @MindHawkHotS 5 лет назад

    The Rigidbody in Attractor doesn't actually need to be public as when a field is set to "private" it is only accessible by that CLASS; not that object. So other objects of the class Attractor can access the Rigidbody even when it's private!

  • @Saidriak
    @Saidriak 4 года назад

    What exactly does "OnEnable()" and "Attractor.Add(this)" do?
    For my script I made the attractors pull in every rigidbody instead of just other attractor (so that the player and physics objects are also pulled in and not just other celestial bodies) and I noticed it only worked with the array method. If I attempted the List way of doing it it would put a red underline under "this" in "Rigidbody.Add(this)" and say "cannot convert Rigidbody to attractor".

  • @JohnWeland
    @JohnWeland 5 лет назад +2

    I'd like to see this reworked to keep a playerObject on the surface of a sphere while still allowing them to move around it.

  • @bdgaminggr1697
    @bdgaminggr1697 4 года назад

    8:35 why not try{}?

  • @Lithobrake0
    @Lithobrake0 7 лет назад +18

    Damn it, after watching this i finally realized whats wrong with my own gravity script. I put it in update, instead of fixed update XD.

    • @cocommander2
      @cocommander2 5 лет назад +1

      I'm in a similar boat. My problem is getting the dimension scaling right so I can maintain realism. My brain gets the actual values used in orbital mechanics calculations confused with the scaled values I'm using for the simulation.

  • @kikemon3455
    @kikemon3455 7 лет назад +1

    Mega like from Mexico!!!

  • @hananabdulrahman6994
    @hananabdulrahman6994 7 лет назад

    You taught me a lot thx :D

  • @Jiririn
    @Jiririn 7 лет назад

    Not even notifications squad, Just Love your videos so much :D

  • @brenoaugusto9791
    @brenoaugusto9791 6 лет назад

    Really amazing!
    Thanks!!

  • @jaydenclark4144
    @jaydenclark4144 7 лет назад +6

    Can you do something on Google Play Services?

  • @servanozcanan5400
    @servanozcanan5400 5 лет назад

    thanks I was looking for this too

  • @nikolaibalck4480
    @nikolaibalck4480 5 лет назад +3

    4:45 - Somebody count how many times he says attract

  • @Gold_d_lion
    @Gold_d_lion 4 года назад +1

    I managed to make 2d balls that attract each other creating a beautiful to look at system but after adding 5-8 rigid bodies the processing power starts to suffer a lot. To the point where it just stops working properly and lags. Does anyone know how I can manage to have more objects that need less calculating power?

  • @quantumbyte-studios
    @quantumbyte-studios 2 года назад

    Was actually wanting to make a video game from this

  • @timbass9822
    @timbass9822 7 лет назад

    Hey Brackeys! How much time do you typically spend making one of these well done and fun tutorials!??

  • @zt64
    @zt64 6 лет назад +1

    How could I use the gizmos to draw the range of gravity?

  • @TomtheMagician21
    @TomtheMagician21 4 года назад

    I didn’t understand anything you just did but it was a cool video

  • @Yorgarazgreece
    @Yorgarazgreece 4 года назад

    I did this a few years ago on my own. I'm was proud when i made it work. Turns out it's no big deal. It's just that I am too dumb to understand physics lol. Also, good to know that static attribute prefix can make the attribute accessible globally to all instances with the same class name.

    • @ImInfenix
      @ImInfenix 4 года назад +1

      In fact, the static keyword is even more powerful. It doesn't allow all instances to access the attribute but creates an attribute which is attached to the class directly. That means that you can also, if marked as public, access to your attribute through Class.staticAttribute and that is just SUPER useful.

  • @Soraphis91
    @Soraphis91 7 лет назад +12

    - should have made use of [RequireComponent] attribute.
    - should have used Vector3.sqrMagnitude, because obtaining the magnitude is way more costly (especially if you're not gonna need it)
    - same goes for Mathf.Pow(s, 2), its a float wrapper around Math.Pow and this is more costly than x * x
    - instead check floats for zero, use Mathf.Approximately()
    was going to write better be using a static list for all attractors, glad to see you solved it the exact way i would have done it.

    • @CJBurkey
      @CJBurkey 7 лет назад

      Thanks!

    • @MoMoGammerOfficial
      @MoMoGammerOfficial 7 лет назад

      You can use Int if you do not care more about the precision.

    • @Sparrow420
      @Sparrow420 6 лет назад

      I was just about to say pretty much that.
      ALSO, for some reason rb.position is expensive vs transform.position(at least for 5.6 and 2017.3), check the difference yourself with this setup, when object count is at the thousands(i have an i7) using rb.position leads to about 1 fps and using transform.position i stay at a cool 50-60(with it's limit ofc)
      have a look at the profiler and dont forget to change both rb.positions if you try this.

    • @Sparrow420
      @Sparrow420 6 лет назад

      Also also, method calling is expensive so try to do pretty much that in FixedUpdate:
      for (int i = 0; i < activeAttractees.Count; i++) {
      Vector3 dir = transform.position - activeAttractees [i].transform.position;
      activeAttractees [i].rb.AddForce (dir.normalized * (G * (rb.mass * activeAttractees [i].rb.mass) / dir.sqrMagnitude));
      }

    • @skutteoleg
      @skutteoleg 6 лет назад +1

      I think it could also be optimized by making one master script in the scene which would control all the attractors, rather than have FixedUpdate be called every time on each of them

  • @SinanAkkoyun
    @SinanAkkoyun 7 лет назад +2

    *Please* explain the optimisation... Why does the list contain _every_ attractor if you only add "this" to it?

    • @baptistehudyma1612
      @baptistehudyma1612 7 лет назад +1

      Put simply, every time you create an object, it will add his attractor component to the list. The list is static, that mean it is common to all the scripts. So every script access the same list and add there own Attractor component to it.
      OnEnable is triggered the first time the script is enabled.

    • @MoMoGammerOfficial
      @MoMoGammerOfficial 7 лет назад

      If we do that, it will add overhead on your game.. why checking it each time when you can just add the living object in the list upon when its just spawned? it improves performance... Coz there are tons of stuff happening all the time and having something silly like this will produce lag in calculations vs response.

    • @DoctorLodi
      @DoctorLodi 6 лет назад

      Because the Attractors property is **static**, it belongs to the Attractor class itself rather than each instance of an Attractor having its own copy. You usually see this most commonly with constants, like Integer.MAX_INT where you're asking the Integer class what its max value is rather than asking an individual Integer, but it still applies in cases where the static property is not a constant.

    • @RoySchl
      @RoySchl 6 лет назад +1

      he made the list static, which means one list is shared by all the attractor objects... so if every object adds itself to a list that everybody can see, they are now all in the list.

  • @MrAdventureHat
    @MrAdventureHat 3 года назад

    Im trying to make a spaceflight simulator kinda like kerbel space program so i was wondering can i use this as a flat plane and if so how whould i go about adding thrust so that my object (rocket) can launch up i wanna do this in a way of adding one of those atmospheric balloons so my plan was to do a thing where i can apply weight and thrust changeable but i dont actually know how to or where to start

  • @hariikrishnan
    @hariikrishnan 7 лет назад

    are you having plans to stream
    Love your vids keep it up

  • @supertenchoo4271
    @supertenchoo4271 4 года назад +1

    why direction.normalized is used there i want to know normalized function

  • @Adamthegeek70
    @Adamthegeek70 7 лет назад +1

    Can this be used for a character controller, thinking planet sized map with a texture generated with libnoise or something like that with a character controller walking on the service, vehicles etc.?

    • @docill9155
      @docill9155 7 лет назад +4

      It can be. You just need to set the mass of the character to be small. It would also be wise to put in some logic to auto-rotate the character so their "feet" are always pointing towards the planet that is applying the strongest force on the character.

  • @MathsPlusGames
    @MathsPlusGames 6 лет назад +1

    Please help, I have error with this line "FindObjectOfType();", it says " cannot implicitly convert type 'Attractors' to 'Attractors[ ]' "

    • @MultiMightyEagle
      @MultiMightyEagle 6 лет назад +3

      Its probably "FindObjectsOfType" instead of "FindObjectOfType". one letter can ruin everything :/

  • @jaycelee8453
    @jaycelee8453 6 лет назад

    wats is the short key to complete the basic foreach statement?

  • @Future_Guy
    @Future_Guy 7 лет назад +3

    Make a follow up video on "Surface Gravity" for cube shaped planets please. :D

    • @ananttiwari1337
      @ananttiwari1337 4 года назад

      Thats relatively easy to the point where you should figure it out on your own.

  • @michaelramczyk4721
    @michaelramczyk4721 4 года назад

    Hey Brackeys, How do I make a XR Rig walk on surfaces, I can't control myself to walk in the different axis, because my XR Rig is not set to local directions, I don't know how to do that. Please help.

  • @JustinEames
    @JustinEames 5 лет назад +1

    This is tremendous. Thank you, as always. I'm a high school teacher thinking of using Unity to reinforce concepts of astronomy and physics, but I'm novice at best. Does anyone know if the Unity physics engine is up to the task of using the method described in the video to create stable orbits? If we were to create a large sun-like sphere and send a smaller planets zooming by, would Unity physics be able to capture them and keep them in anything like a stable orbit? I feel like that's probably a pretty big ask of the physics engine.

    • @robertwakeling
      @robertwakeling 5 лет назад +2

      I reckon it would be up to the challenge. I'm also thinking of modelling projectiles (I teach Maths and think it would be cool to have a way for the kids to actually model the process and get approximate answers)

    • @clamhammer2463
      @clamhammer2463 4 года назад +1

      Unity would be up for it. It all depends on your skill of implementing it. I'd imagine that it'd be easier to find a pre-made solution already out in the wild. Surely what you're looking for has already been created and more than likely by a professional coder/designer. if your goal is to learn unity and the underlying tech, then by all means, work through it. The tech can handle it.