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. Dismiss Notice

Planet-Like gravity

Discussion in 'Scripting' started by bora155, Apr 14, 2021.

  1. bora155

    bora155

    Joined:
    Mar 31, 2020
    Posts:
    12
    So , i wanna remake a game that no one knows , soosiz . It was my childhood game and imo its the best platformer for its unique gameplay back in its time . I basically want to make that the player will snap to the platforms surface , making a planet-like gravity . I would appreciate it if you helped me how i could snap an objects bottom surface to a platforms surface by maybe a youtube tutorial , some code samples or sth :) Soosiz.png
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You first need to find what object is effecting you (basically by shortest distance, but you can make it so bigger object have more influence)

    To apply gravity you just find the direction to it from and and apply a force based on the distance (you can use newton's famous equations for this)

    To snap to the surface:
    1) cast a ray to said object
    2) set player position to the raycast hit position (assuming the player's pivot is at the feet)
    3) set player.transform.up to the raycast surface normal


    I'm not sure how that translates to 2D (i'm a 3D guy), but it should be pretty samey
     
  3. bora155

    bora155

    Joined:
    Mar 31, 2020
    Posts:
    12
    3) set player.transform.up to the raycast surface normal

    Thats probably what im looking for . Thanks for the quick reply!