Unity Tutorial - Solar System with Unity Physics

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ม.ค. 2025

ความคิดเห็น • 113

  • @mezan2604
    @mezan2604 2 ปีที่แล้ว +7

    Honestly this is the best tutorial about this topic

  • @garcia7953
    @garcia7953 2 ปีที่แล้ว +24

    Why is your channel so small?
    Great content!
    You deserve so many subscribers, i can understand something so complex in just some minutes, that is amazing!
    Congratulations.

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว +5

      Thank you so much it means a lot!

  • @StellarMirage
    @StellarMirage 3 ปีที่แล้ว +5

    I knew the formuals and everything but wasn’t ready to implement it but after seeing your video I got the faith in myself to implement it and now I have my own solar system.

  • @t2g4_channel
    @t2g4_channel 2 ปีที่แล้ว +4

    Whoah, this is awesome!
    Thank you! Super video, it is short, explaining a lot, and have clear demostration!

  • @LordxGx07
    @LordxGx07 ปีที่แล้ว +1

    A beautiful simulation with physics formula on Unity, beautiful !
    I'll reproduce your code I guess. And after that, I'd like to add code to simulate planetary rings (or proto-solar disc, or accretion discs), like Saturne, Jupiter...

  • @rohithkumar2576
    @rohithkumar2576 ปีที่แล้ว +1

    Is there any way to download the assets directly? Someone please reply

  • @coderious4568
    @coderious4568  3 ปีที่แล้ว +3

    There is an error in the slide at 3:46, the elliptic orbit speed is the square root of G * m2 * (2/r - 1/a) where a is for the alpha in the formula. It is the length of the semi-major axis of the elliptical orbit (not a the game object).

    • @misa8286
      @misa8286 3 ปีที่แล้ว +1

      This doesn't work for me this fix. it says "Operator '/' cannot be applied to operands of type 'int' and 'GameObject'"
      how do i fix this? and can i contact you?

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว

      @@misa8286 there a link to Discord in the channel description, feel free to contact me over there

    • @bensawiuk7094
      @bensawiuk7094 3 ปีที่แล้ว

      @@coderious4568 im having the same problem, where do i contact you

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว

      @@bensawiuk7094 There is a Discord link on my page description

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว

      @@misa8286 I added a clarification. "a" is the for the Greek letter alpha in the formula, not for GameObject a from the foreach loop. The alpha is the semimajor axis of its orbit.

  • @HaveDraw
    @HaveDraw ปีที่แล้ว +1

    What if i dont have the transform.right, how could i do the same thing?

    • @coderious4568
      @coderious4568  ปีที่แล้ว

      How could you not have transform.right? All GameObjects have a transform.

  • @smoothblock7009
    @smoothblock7009 2 ปีที่แล้ว +3

    hi , nice video but i have a question - when using urp my textures dont apply to my object. any solution please??

  • @ytubeanon
    @ytubeanon 3 ปีที่แล้ว +2

    quick tip - I think if you press Shift+F it will lock on to your selected gameobject in Scene view, so you don't have to manually scroll the screen with your mouse

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว

      Excellent, thank you for the tip!

    • @Enderia2
      @Enderia2 ปีที่แล้ว

      @@coderious4568t’s actually just f

  • @tntram2834
    @tntram2834 2 ปีที่แล้ว +1

    Very useful video!
    It would be interesting to see what could be done from this project

  • @ComplexKangaroo
    @ComplexKangaroo ปีที่แล้ว +3

    Excellent tutorial, thanks! I got it working in Unity 2021.3.24f1 but my code is identical to yours and all the RigidBody mass settings and I turned off "use gravity" as you did, but the Earth takes off on a long comet like orbit. I had to multiply the initial velocity by 0.7 to get a perfect circle, which is weird. I just realized 0.7 is 1/sqrt(2), and indeed adding 2*r solved it and makes it work, but your code didn't have that and your code matches Wikipedia. Weird. Thanks for all the other cool tricks with TrailRenderer and speeding up project time.

  • @pikachuiswatchingu601
    @pikachuiswatchingu601 2 ปีที่แล้ว +3

    hi this was a wonderful tutorial but im having a problem where after i added more planets the sun started to wander. like it wanders in a straight line and pretty fast too. i get that it gets pulled a little by the other planets but it's literally wandering to a whole nother galaxy

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว +2

      Checking the IsKinematic checkbox in the sun Rigidbody will not apply physics on the object.

  • @havendewart
    @havendewart 2 ปีที่แล้ว +2

    Just wondering why did you add two foreach loops. I am sure there is a good reason, but I just cant figure it out and I am trying to learn C# so was curious.

    • @gilleswalther5964
      @gilleswalther5964 2 ปีที่แล้ว

      Because gravity goes both ways, A is "attracted" by B and B is "attracted" by A. With the double loop both ways can be captured.

    • @havendewart
      @havendewart 2 ปีที่แล้ว

      Thanks!

  • @CheesyMez
    @CheesyMez 4 หลายเดือนก่อน

    thanks for such a good tutorial!
    would you know how to have the trajectory of an object ahead of time, rather than trailing behind the objects
    like in kerbal space program for example.

  • @ouuuaburger2093
    @ouuuaburger2093 ปีที่แล้ว +1

    Do you think blender is better than Unity in rendering solar system?

  • @tracksuitcheems
    @tracksuitcheems 2 ปีที่แล้ว +4

    Greetings. I followed your tutorial for the shader graph star and this one for the orbits. However, my planets are not dynamically lit. Could you do a video on lighting in Unity or recommend any good ones. Soulcraft looks good, btw. Hope to see it released soon. Thanks.

  • @williamwall1785
    @williamwall1785 2 ปีที่แล้ว +7

    Cool stuff! However, there is an error in the formula for velocity in an elliptical orbit. The (2/r + 1/a) should be (2/r - 1/a). Also, on the distance scale chosen, the earth and the sun would be fifty times smaller.

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว +2

      Spotted it too, I've pinned a comment with the correction.

  • @thedarkshadoww9711
    @thedarkshadoww9711 2 ปีที่แล้ว +1

    Ty so much for the tutorial, helps a lot

  • @patrudoizero
    @patrudoizero ปีที่แล้ว

    I`m trying to create a deformable 3D space time grid in unity. How should I approach it?

  • @Avariths
    @Avariths 3 ปีที่แล้ว +3

    So I have a weird problem with the velocity. When I add the velocity part to the script my earth just flies away. I copied the script line for line. Any idea why this is happening to me?

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว

      Did you add mass to the sun?

    • @Avariths
      @Avariths 3 ปีที่แล้ว

      @@coderious4568 yes. I put the mass of the sun at 333000 and the earth at 1. Gravity of both planets turned off

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว

      @@Avariths Did you add the tag to the celestials?

    • @Avariths
      @Avariths 3 ปีที่แล้ว

      @@coderious4568 I gave the tag Celestial to the sun and Earth. The gravity part of the script functions as intended. The sun also starts to move away from the Earth when the velocity component is added along with the earth shooting away

    • @bennettpoh
      @bennettpoh 3 ปีที่แล้ว +1

      Did you apply the script to the planet itself? If so, you have to create an empty GameObject and assign the script there.

  • @divac3042
    @divac3042 2 ปีที่แล้ว +1

    It's so cool! Thank you very much!

  • @liliansananikone3588
    @liliansananikone3588 ปีที่แล้ว +1

    In realtime, is the G float member different for each celestial ?

    • @liliansananikone3588
      @liliansananikone3588 ปีที่แล้ว

      I did the same way with all celestials of your spreadsheet but some celestials end to collide (Sun, Mercury and Earth).

  • @tamargamer4667
    @tamargamer4667 ปีที่แล้ว +1

    Where can i find a 3d models like These planets and the sun are wonderful

    • @coderious4568
      @coderious4568  ปีที่แล้ว +1

      Thanks, I made a tutorial on how to create the Sun. Planet textures can be found on the NASA website and are public domain.

  • @JakubKas
    @JakubKas 2 ปีที่แล้ว

    I get " operator '.' cannot be applied to operand of type 'void' " error message :/

  • @Maxvellua
    @Maxvellua 2 ปีที่แล้ว +1

    Thank you for the video!

  • @snipeax0478
    @snipeax0478 2 ปีที่แล้ว +2

    thank you, that helped me so much to figure some problem with my project, would it be possible to make a video on how to predict the trajectory of a planet with a linerender or something?

  • @Baconmaster-ls4wf
    @Baconmaster-ls4wf 9 หลายเดือนก่อน

    Anyone able to help I have implemented this but the planets do not revolve around the sun they just start going all over the place

    • @coderious4568
      @coderious4568  9 หลายเดือนก่อน

      The project is available on GitHub link under the video if you want to compare your solution to the example.

  • @overangelixvod1406
    @overangelixvod1406 ปีที่แล้ว

    Hello, I want to make a solar System but I understant why do you increased the earth mass by 15 ?

    • @coderious4568
      @coderious4568  ปีที่แล้ว

      It's to make it work with the scale of the planet. To make it look better the distance and sizes of the planets are modified, so the mass needs to be adjusted as well.

  • @jeronimocollares320
    @jeronimocollares320 2 ปีที่แล้ว +1

    Nice tutorial, really thanks. But you have something about a 'HUGE' system star, Solar or, at last, what is the best way to do it without broke the engine (because this size of a space, planets, etc)?

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว +1

      Hi, thanks for your comment. To make a huge system I would look into Compute Shader and ECS/DOTS. The two options are completely different but both allow to perform a ton of operations per second. I made a tutorial on how to use the compute shader, it might be suitable for what you have in mind. With ECS/DOTS Unity can manage thousands of different objects at the same time with much better performance than using the GameObject/Monobehavior framework. it is still a preview package though.

    • @jeronimocollares320
      @jeronimocollares320 2 ปีที่แล้ว

      ​@@coderious4568 Thanks for answering. I'm working with a 'middle ground' on the 'huge' theme. Instead of thinking in terms of 'huge', use tricks like scaling something down to avoid rendering and performance issues. There will be many steps, I'm still a novice - I know nothing about DOTS, I would like ideas, even. Now I focus on movement, like having a spacecraft, for example, flying in 'cruise flight' with this spacecraft being small and simulating that flight as something very slow and maintaining a good 'frame by frame' quality? That's my challenge, learn that. A smaller Universe, with small objects, pretending to be big. Another thing I've been looking for is light, like a general lens flare in this universe and combine it with this Sun used in your example. I know it's newbie stuff, I apologize - but that's how we learn. :)

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว +1

      @@jeronimocollares320 To render big lands, procedural generation could help as well. I made a video about it. The concept is in 2D, but it's totally applicable to 3D as well.

    • @jeronimocollares320
      @jeronimocollares320 2 ปีที่แล้ว

      @@coderious4568 my idea is simple (as a new) its a space game using point and click - literally, like Eve Online in your BASIC aspect (for now, strict to two goals. Size of a sector in the Universe (objects included) and movement (here it goes from the movement of objects navigating in a three-dimensional space to that of the camera).

  • @myfishisnothappy1595
    @myfishisnothappy1595 3 ปีที่แล้ว +1

    could you project shadows and add a fixed pov from earth surface? Even without earth rotation on its axis, to see the Moon phases? That would be amazing...

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว +1

      Add a point light to your sun and it will cast shadows. URP does not support real-time shadow from point lights, but HDRP does.

  • @TheRunningSongs
    @TheRunningSongs 3 ปีที่แล้ว +1

    I followed the gravity script but when i compile the script i get two errors saying ';' and '{' I double checked everything and nothing is working

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว

      What code editor do you use?

    • @TheRunningSongs
      @TheRunningSongs 3 ปีที่แล้ว

      @@coderious4568 Visual code studio, same as you

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว

      @@TheRunningSongs You can reach out on the Discord channel and post your code. There is a link to the Discord on the description of my page.

    • @TheRunningSongs
      @TheRunningSongs 3 ปีที่แล้ว

      @@coderious4568 Hello, Thanks fir the response, when i jooin the discord server it says invite invalid. If you could update the invite that would be great, thanks :)

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว

      @@TheRunningSongs discord.gg/KsgpVVYcj9 updated

  • @taxtherich194
    @taxtherich194 ปีที่แล้ว +1

    thank you for this great tutorial.. merci

  • @hue7075
    @hue7075 2 ปีที่แล้ว +1

    Awesome stuff, how can I make the planet movements significantly slower but still make it work? new to coding sorry lol

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว

      The speed can be adjusted with the G variable.

  • @choc3732
    @choc3732 2 ปีที่แล้ว +1

    Hey man, I have gotten it to work with the circular orbit! However for the elliptical it's not working. Seeing as this solar system is scaled down, what values do I need to use for the values: 2, 1, a in this part of the equation -> (2/r - 1/a)?

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว +1

      r = distance between orbiting bodies
      a = length of the semi major axis

    • @choc3732
      @choc3732 2 ปีที่แล้ว

      @@coderious4568 Thanks man. Do you know what factor I have to scale down the “a” value by? The actual value for a is about 100,000,000km. I’m not sure how to scale it down to suit this smaller solar system.

  • @mukapochotordo649
    @mukapochotordo649 2 ปีที่แล้ว +3

    Hi, could you make a tutorial to make the orbit in the shape of an ellipse? Very good tutorial by the way.

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว

      The the formula for an ellipse is pinned in the comments. Thanks 👍

    • @mukapochotordo649
      @mukapochotordo649 2 ปีที่แล้ว

      @@coderious4568 ty so much!

  • @sujalvc
    @sujalvc 3 ปีที่แล้ว +2

    Nice video.
    Is deformation of planet w.r.t the nearest Celestial possible?
    Like if a planet goes near the sun, it will deform and vapourise.

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว +1

      It should be possible in combination with the particle system.

  • @supercalifragilisticexpial9124
    @supercalifragilisticexpial9124 14 วันที่ผ่านมา

    Veeery cool, thank you so much :)

  • @Keith-8h1a2b9i
    @Keith-8h1a2b9i 3 หลายเดือนก่อน

    is this used only for circular orbits, or can it make elliptical too?

    • @gilleswalther5964
      @gilleswalther5964 3 หลายเดือนก่อน

      Works with elliptical orbits

  • @R2_Audio_
    @R2_Audio_ 3 ปีที่แล้ว +2

    hi! this is a awesome tutorial, im making my own solar system simulation too, can i get the codes and the mass tables from somewhere?

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว

      Masses and distances can be found on google quite easily

  • @JohananStarGiver
    @JohananStarGiver 11 หลายเดือนก่อน

    How did you make that beatifull sun? i cant find a way to make it this beatifull :(

  • @alvaropazmino672
    @alvaropazmino672 ปีที่แล้ว +1

    WOWOWOWOW!

  • @brotkrumeleintopf7749
    @brotkrumeleintopf7749 ปีที่แล้ว +1

    5:07 but your sun is moving away from the center?!

    • @chairchairyes2524
      @chairchairyes2524 หลายเดือนก่อน

      although it takes a lot longer than this, the sun does move, due to the mass of other planets.

  • @Gaisiran
    @Gaisiran 5 หลายเดือนก่อน

    Excellent!

  • @damarkuncoro
    @damarkuncoro 2 ปีที่แล้ว +1

    i love it

  • @mjc7747
    @mjc7747 3 ปีที่แล้ว +1

    Thank you soooo much!!

  • @fatimaarif5944
    @fatimaarif5944 3 ปีที่แล้ว +1

    thanks life saver. < 3

  • @abdelrahmanqe-5538
    @abdelrahmanqe-5538 10 หลายเดือนก่อน

    Very Useful Vid

  • @baskanaqua
    @baskanaqua 2 ปีที่แล้ว +3

    ur code working so good uhm how can i ask.. can i use this my education project?

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว +2

      Yes you can use it

    • @baskanaqua
      @baskanaqua 2 ปีที่แล้ว +1

      @@coderious4568 i need to thank u!!! SO SOOO thank u so muchh ur so good

  • @yaxalo
    @yaxalo 3 ปีที่แล้ว +6

    I dont know if this is just me but my planets like to go out of orbit and shoot off into space.

    • @coderious4568
      @coderious4568  3 ปีที่แล้ว +4

      The script must be attached to one GameObject in the project not all planets. That might be the cause.

    • @R2_Audio_
      @R2_Audio_ 3 ปีที่แล้ว

      @@coderious4568 hi, i have the same issue can you help?

    • @DrDandD
      @DrDandD 3 ปีที่แล้ว

      ​@@R2_Audio_ have you tried what Coderious said above.

    • @cfg4522
      @cfg4522 2 ปีที่แล้ว

      Make sure that InitialVelocity function is being called in the Start method and not in FixedUpdate

    • @chiefynuts
      @chiefynuts 2 ปีที่แล้ว

      @@coderious4568 works perfectly. was confused for so long.thank you!

  • @thegamedevstudent
    @thegamedevstudent 2 ปีที่แล้ว +1

    instead of the planet moving towards the sun, the sun is moving towards the planet in the gravity part. cud u pls lend a hand

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว

      Have you double checked the mass of your objects?

    • @thegamedevstudent
      @thegamedevstudent 2 ปีที่แล้ว

      @@coderious4568 yes, the sun has mass of 333000 while the planet has mass of 1, still the problem is occurring

    • @coderious4568
      @coderious4568  2 ปีที่แล้ว

      @@thegamedevstudent double check the inputs, make sure gravity is off the rigibodies, that it is not kinematic and that the Celestial tag was added. Then use the debugger to check what is wrong :-) Good luck

  • @RiversJ
    @RiversJ 2 ปีที่แล้ว +1

    Err i don't mean to be pedantic but i don't see Unity Physics being used here and i'm presuming this is using the built-in Physx physics engine?

  • @KassidyJMoore
    @KassidyJMoore ปีที่แล้ว

    I can't get the moon to orbit the planet

  • @myfishisnothappy1595
    @myfishisnothappy1595 3 ปีที่แล้ว

    I luv you.

  • @sanyohamarr8285
    @sanyohamarr8285 2 ปีที่แล้ว

    As expected, it doesn't work. Oh well, another tutorial fail.

    • @Brainyplanet
      @Brainyplanet 2 ปีที่แล้ว

      It worked for me, maybe you missed something

    • @Excelsiur1
      @Excelsiur1 ปีที่แล้ว

      This worked perfectly for me as well. Go step-by-step and double check all of your settings and values. Seems to be working for everybody else.

  • @gizmo95
    @gizmo95 2 ปีที่แล้ว

    It seems like the initial velocity makes the sun drift! I added a simple if statement to check if it's the sun. this seemed to fix it.. although a bit of a hack!
    if(a.name != 'sun'){
    a.GetComponent().velocity += a.transform.right * Mathf.Sqrt((G * m2) /r);
    }