Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

2D Faux gravity. Help

Discussion in 'Scripting' started by mrhagge, Oct 13, 2018.

  1. mrhagge


    Nov 22, 2017
    Hello, I'm wanting to make a script for walking on planets in 2d with gravity like it. Example

    Please help me on how I could achieve this in c#.
  2. Kurt-Dekker


    Mar 16, 2013
    At a glance there's a few things going on:

    1. gravity is angled for each actor to point to a central point (which planet they're on)

    2. actors rotate to align to the ground beneath them

    3. the camera is rotated to follow a given actor

    You can do #1 by taking the difference between the planet and the actor and using that vector as the basis for gravity (normalize it first, then multiply by the desired gravity)

    You can do #2 by using rotating your character so it aligns with the delta calculated in step #1 above.

    You can achieve #3 trivially by parenting the camera to whatever actor is the main player actor.

    That should get you started anyway. Let us know how it goes!