Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Variable Gravity Area?

Discussion in 'FPS.Sample Game' started by Stexe, Mar 4, 2019.

  1. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    I'm trying to figure out how to create a variable gravity area. Such that when an object (e.g. the player) moves into the Box Collider trigger area the gravity acting on that object changes. The main goal is to simulate being on the moon or something similar in certain areas, so having it act on the player is key, but having it also trigger on other objects (like grenades) would be neat too.

    I took a look at the "Damage Area" for an idea on how to create trigger zones that do things, but I can't figure out how to manipulate a player's gravity in said field.

    Granted, I have zero experience with ECS so everything is very confusing with that framework. Plus, I'm more of a game designer than a programmer, so even figuring out the code without ECS would have probably been challenging.

    Any help would be greatly appreciated. Thanks!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,630
    There is few ways of doing it.

    One for example, when in area of gravity influence, then disable in rigid body gravity flag, and apply RigidBody.AddForce, or AddRelativeForce.

    But in general case, I would turn off completely default gravity and only apply Add force / relative as gravity.
    Then you just change value of gravity force, depending if in, or out of the area.
     
    d12duke likes this.
  3. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    Yeah, I understand the logic behind some of the methods to achieve it... the problem is implementation using the ECS system. I have a very, very basic idea of how it works by taking apart the "Damage Area" code, but even then I'm unsure how to manipulate the gravity of objects that enter said field.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,630
    Oh, my apology, I missed completely ECS part in your initial post, unless that part has been edited.

    But ECS may be much more difficult to grasp, if not having decent familiarity with programming itself.

    Using ECS, you need some form of cubical area check. Perhaps need only intersecting AABB (Axis Aligned Bounding Box). So basically, you need area Bounds, with intersecting bounds of character, or point. Even simpler, would be using radius of the sphere. Then you can trigger system using IComponentData for example, by adding, removing this component to entity.

    But since using RigidBodies, you will be using Hybrid ECS for that part, since you need AddForce.

    Then unless you are familiar with differences between Classic OOP / Hybrid ECS / Pure ECS, this may give you a bit headache, to figure out for none programmer.

    But should look for anything gravity related, in case if there is anything already.
     
  5. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    I didn't edit my original post, I was always talking about ECS since that's how the "Damage Area" code works and I figured to be safe in multiplayer and following the build guidelines I should follow that formatting.

    I found "physics" mentioned in a few places, but unsure exactly how to parse the information and access it in how it interacts on objects. Some notable places that I think might be important are:
    But I have no idea exactly how to formulate it. I would assume I could take the value of the object that comes into contact with the "Gravity Area" and check to see if gravity acts on it and then manipulate their said gravity. For example, for the player when they enter the "Gravity Area" it takes their Character info and passes the ability to modify "Ability_Movement.cs" and specifically changing " Game.config.playerGravity; " while in the field. But the specific code on what to write and how to formulate it, especially since it is ECS, escapes me.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,630
    Line 225 in
    https://github.com/Unity-Technologi...cter/Behaviours/Abilities/Ability_Movement.cs
    Code (CSharp):
    1. var gravity = Game.config.playerGravity;
    May be what you want to manipulate.
    This is still however, on main thread, as runs in System Update.
    However, if anything, you want change gravity, not the Game.config.playerGravity.

    Btw., the format is ECS alike, since systems are used, but still uses mostly Classic OOP APIs.
     
  7. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    I mentioned that in my above post about changing the player gravity.

    I'm unsure how you would change gravity in a specific region though instead of on an object on object basis.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,630
    Yes, I rather justified, to be clear.

    This isn't something you can do with simple one liner code unfortunately. You really need get to understand multiple aspects, regarding programming and Unity, to be able accomplish what you need. Is not that difficult what you are asking for, but it may be a bit of work. If you are familiar with C# you will figure out. But if your knowledge is basics, giving any guidance will result avail, as you will have missing critical bits of experience.

    Other than that, I suggest stay with Unity Classic OOP. Will be much easier for you to do.
     
  9. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    I know enough programming to be able to do it. Just ECS is very different in concept. If it wasn't ECS I'd be able to do it easily. Was just reading out here to see if anyone else has figured something out with FPS Sample and how to do something like this.
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,630
    You can pass desired argument to the CalculateMovement and Update, which should be relevant to gravity value, instead gravity settings for example. Or some argument to trigger switch.
    You could even use static values if you like. There is really very little to ECS in https://github.com/Unity-Technologi...cter/Behaviours/Abilities/Ability_Movement.cs case.

    So you will need just simply create GameObject cube as gravity area, as normally you would do and you need use OnCollisionEnter, OnCollisionExit, to switch gravity value, to pass as argument to move method. And place this GameObject, somwhere in the scene.
     
  11. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    Yeah, that's what I was doing until I saw how the ECS is set up and it doesn't seem to work that easily. CalculateMovement doesn't use Monobehaviour so when I make the "gravity" variable public and try to access it I get the error:
    ArgumentException: GetComponent requires that the requested component 'Movement_Update' derives from MonoBehaviour or Component or is an interface.
    UnityEngine.GameObject.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/GameObject.bindings.cs:28)
    LowGravityArea.OnTriggerEnter (UnityEngine.Collider c) (at Assets/LowGravityArea.cs:31)


    I think I can mimic how "DamageArea" does it with "DamageAreaSystemServer" but I just hope it doesn't open a can of worms that I need to go down.
     
  12. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,630
    Out of interest, did you try pass static gravity variable directly to the movement method? But Update should be able accept any incoming variables. Try use Debug log there.
     
  13. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    Yeah, it looks like just taking out one value (Game.config.playerGravity), changing it, then setting "gravity" to it in each PlayerMovement update it kind of worked. The problem I have now is that it updates the gravity for every player instead of just the one who goes inside the collider. Not sure how to access just the one that enters the trigger collider as opposed to changing gravity for all players.

    Strangely, the way they have gravity acting on the player and how movement works leads to some interesting results. Jumping and hitting your head on a flat collider causes you to stop falling downward. I assume it thinks you're "walking" upside down, as the second you leave the flat collider you start falling again. Luckily that isn't relevant in my case, but I could imagine it being an issue for others.
     
  14. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,630
  15. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    Tried something similar to that but couldn't figure out what data to pass and how to exactly pass it. I was following along the DamageArea script but it led me down a rabbit hole with server verification of double checking data and more.
     
  16. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,630
    Unfortunately I look only through github, as I don't have project installed. Hence I can not look and trace references, where they are going / coming from.

    So I am afraid, that is it of my capabilities, to assist.
     
  17. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    Tried passing variables between the scripts and setting up something similar to how DamageArea works, but just passes a single bool value. Lots of problems with that and no idea how to deal with it, even more so since it is ECS based.

    I also found some other weird issues when changing gravity the way it is implemented since it is tied into other factors. Basically if you jump at your max height you'll stop at the apex and you won't fall down until you start moving again. Also, it seems like Double Jump and Rocket Jump can mess up gravity if you change it.

    Way too big for me to deal with right now so I posted some comments to people who have more experience with ECS and FPS-Sample. Hopefully they respond so I can figure out this... have a decent sized project riding on it.
     
    Antypodish likes this.
  18. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    Anyone else have any success? I've done a lot of other things but so far this one has eluded me.
     
  19. d12duke

    d12duke

    Joined:
    Mar 22, 2016
    Posts:
    32
    I haven't tried it but I read through your issue and I could suggest what I'd do.. Though I don't know how ECS would pull into this... I'd create trigger and constrain it by that area and providing your player movement is setup, I'd change the "gravity" of that area to -1 within those constraints.

    Edit: I googled this and it pretty much says the same thing but explains it much better.
     
  20. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    The problem is more than just changing the gravity too. The way they do their movement and jump calculations has some other weird effects. For example, if you just change the gravity for everyone you'll run into problems where if you jump and hit a flat surface with your character's head you'll stop falling down entirely. Since I assume it thinks you're "walking on the ceiling" or something. Also, if you jump to max height but don't move, you'll just just stay there hovering until you move in which case you then fall. Lots of weird little things like that happen if you try messing with gravity or some of the jump settings.
     
  21. keeponshading

    keeponshading

    Joined:
    Sep 6, 2018
    Posts:
    181
    Last edited: Apr 17, 2019
  22. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    They have gravity set up. The problem is more accessing and editing the ECS and their code and making it work between servers and stuff. I don't want to have to re-write all the stuff from scratch.

    Also, a lot of other systems they have set up are based off of how they use gravity to calculate like height of jump and stuff. Seems like it is a lot more than just changing Gravity Factor or something.
     
    Last edited: Apr 18, 2019
  23. keeponshading

    keeponshading

    Joined:
    Sep 6, 2018
    Posts:
    181
    I am pretty sure that the new unity physics stuff will be integrated in one of the next fpssample updates and then physics behaviour and ragdolls should be simply registered for multiplayer.

    All what you see in the GDC video should be possible with
    latest big updates on Github Repositorys the last few days from

    Unity ECS and Unity Physics
    https://github.com/Unity-Technologies/EntityComponentSystemSamples?files=1
    and
    Unity Multiplayer
    https://github.com/Unity-Technologies/multiplayer?files=1

    Hope there will be an update soon for Unity 2019.1.
    where all comes together.

    I stepped little back to these simple examples because complete Unity FPSSample team stopped nearly every forum support around 4. december last year. Don t know why.
    But must have a reason.
     
    Last edited: Apr 18, 2019
  24. d12duke

    d12duke

    Joined:
    Mar 22, 2016
    Posts:
    32
    Hmm.. If the collider hits the collider it stops working? That sounds weird, but if that's the case then I'd do another bool and if they're in that area, then if the 'gravity' for that trigger changes in the frame, reset it back to normal... It's a dirty fix but it's all I got on first thought. (Sorry man, head deep in netcode atm and stuck af:( )
     
  25. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    The problem is passing a bool for "on collision" in an environment without messing up other things. Plus, even if I could get it to change gravity there are still issues with things like not falling down at the apex of your jump if you don't move.
     
  26. d12duke

    d12duke

    Joined:
    Mar 22, 2016
    Posts:
    32
    I mean you wouldn't have things fall no? It's in a ZeroG environment right? I got a couple minutes, let's figure this out. You got discord?
     
  27. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    62
    Yeah, my account is "Stexe#6656" but I'm not sure I have time right now to go through and debug it all at the moment.
     
  28. d12duke

    d12duke

    Joined:
    Mar 22, 2016
    Posts:
    32
    ahh. Okay, i'll add you.. (do NOT share my discord with anyone! Like, Not even if they say they're unity folk!)