Search Unity

I need help with adding gravity in a different way.

Discussion in 'Scripting' started by ktkimbrough4, Oct 24, 2021.

  1. ktkimbrough4

    ktkimbrough4

    Joined:
    Oct 24, 2021
    Posts:
    5
    Hi! I would like to find a way to basically add a function to the character so that on click, the character gains a massive gravitational pull. The more I research the harder it seems to be, and unfortunately every time I search "gravity" and "character", the only things that come up are character controllers. Any help would be much appreciated!
     
  2. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    So you can play around with the rigid body of the player. Did I get it right?
     
  3. ktkimbrough4

    ktkimbrough4

    Joined:
    Oct 24, 2021
    Posts:
    5
    More like I want the player to be able to click and things on screen will gravitate towards them.
     
  4. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Ok, if i get it right, you want to make a button (like a thing or anything else in the scene) and if the player clicks on it, the gravity should change?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Are you looking for actual physics gravity simulation, where objects coming towards you bang into each other and bang into you?

    Or are you just trying to implement a simple "hoover all the nearby gold objects into the player" effect?

    Those are two completely different things and while you could use similar approaches to solve it, it is better if you know what you're trying to accomplish so you don't waste time fighting features you didn't need.
     
  6. ktkimbrough4

    ktkimbrough4

    Joined:
    Oct 24, 2021
    Posts:
    5
    Excellent question! Ideally, I would be trying to have a realistic feel like your first suggestion where objects can bang into each other.
     
    Kurt-Dekker likes this.
  7. ktkimbrough4

    ktkimbrough4

    Joined:
    Oct 24, 2021
    Posts:
    5
    Correct. The gravity would center around the player.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    So the problem with you asking this is that we've no idea about your current set-up. Sure, all this is possible but are the things you're pulling physics-based or not i.e. do they have Rigidbody(2D) on them etc? It's things like this you should try to clarify when you're asking rather than wait for others having to guess and ask what it is you want. We don't even know if this is 2D or 3D physics for instance.

    So for 2D, this effector can do exactly what you want: https://docs.unity3d.com/ScriptReference/PointEffector2D.html

    For 3D, you'd need to perform a query like OverlapSphere, gather all the Rigidbody into a unique list of Rigidbody then apply a force towards your player.

    NOTE: There is a dedicated physics sub-forum for this stuff.
     
  9. ktkimbrough4

    ktkimbrough4

    Joined:
    Oct 24, 2021
    Posts:
    5
    Thank you for all the insight! Next time I have a question, I will bring it up in the proper space. I am working in 2D with objects effected by Rigidbody2D. Once again, thanks!