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.
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.
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.
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.
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.
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.
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.
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.
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.
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:
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.
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.
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.
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.
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.
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.
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.
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.