Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

2D Faux gravity. Help

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

  1. mrhagge

    mrhagge

    Joined:
    Nov 22, 2017
    Posts:
    4
    Hello, I'm wanting to make a script for walking on planets in 2d with gravity like it. Example http://www.adiosgame.com/

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

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    3,549
    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!