Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    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. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    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!