Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Multiple Sources of gravity.

Discussion in '2D' started by TheFastOne, Dec 28, 2022.

  1. TheFastOne

    TheFastOne

    Joined:
    Dec 20, 2022
    Posts:
    6
    You can click planets around the screen, which pull the player towards them. I want there to be multiple planets, which all work in harmony. I am relatively new to Unity, and have literally no idea how to implement these features. From basic knowledge acquired by coding some of the basics, and by Googling questions I had, I tried coding the embedded script. It didn't work, and Googling some of the basics only led me to 3D answers or videos/threads that had the question all wrong!

    Screen Shot 2022-12-27 at 5.37.19 PM.png

    (Admittedly, a lot of this code was from Google)
    (Before anyone says anything, yes the file was renamed from GravitationalPull to GravitySimulator)

    This script is applied to the prefab of the planet. By the way, I already have the system of clicking and planets appearing down. Also had no idea about that, but Google led me to a good answer relatively quickly.

    Both Scripts: https://github.com/TheSuperFastOne/SecondLearningProject

    Screenshots:

    https://ibb.co/album/jJByLm


    Truly hope these resources are sufficient.


    Help would be most appreciated!
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Coupla things... I think your gravitational attraction computation is off... formally it goes as one over radius squared.

    https://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation

    After you fix that, time to avoid doing GameObject.Find() and GetComponent in Update(), but you can save that step until you get the attraction math working. Try it with two objects and the player.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Try looking at the PointEffector2D which will do this for you automatically. It even has an Inverse-linear and Inverse-squared force falloff.

    Try my PhysicsExamples2D GitHub Repo (see my signature) which has examples for most 2D physics components etc. There's a scene for the PointEffector2D too.

    NOTE: Don't do things like add forces per-frame as you're making it frame-rate dependent. If your frame-rate is high, you'll add more force. This likely isn't what you want.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    What Melv is saying is, use FixedUpdate() for all physics / physics2D purposes.

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    Two good discussions on Update() vs FixedUpdate() timing:

    https://jacksondunstan.com/articles/4824

    https://johnaustin.io/articles/2019/fix-your-unity-timestep
     
  5. TheFastOne

    TheFastOne

    Joined:
    Dec 20, 2022
    Posts:
    6
    Wow, thanks so much! I was getting completely incorrect information on this!