tastysushifish

Unity How to make the mass of an object increase the longer it's air born? (C#, Unity)

Recommended Posts

tastysushifish    106
Using 3D physics how can I increase an object's mass the longer it's in the air.
what would be the best way to go about this? 
 

"for some context, I have a character who's movement is based off exerting force

in a set direction. currently, it can be pushed infinitely and I want to prevent

the player from pushing the character into the stratosphere. "
 
I am relatively new to coding and I am currently using Unity and scripting in C#.
 
Thanks for any help if possible.  :D 
 
Best wishes,
Thomas E.
 
 

Share this post


Link to post
Share on other sites
swiftcoder    18437

"for some context, I have a character who's movement is based off exerting force in a set direction. currently, it can be pushed infinitely and I want to prevent the player from pushing the character into the stratosphere. "

 
I don't think increasing the mass is going to have the effect you desire. Quoting from the Unity docs

"A common mistake is to assume that heavy objects fall faster than light ones. This is not true as the speed is dependent on gravity and drag."

Share this post


Link to post
Share on other sites
felipefsdev    360

"for some context, I have a character who's movement is based off exerting force in a set direction. currently, it can be pushed infinitely and I want to prevent the player from pushing the character into the stratosphere. "


I don't think increasing the mass is going to have the effect you desire. Quoting from the Unity docs:

"A common mistake is to assume that heavy objects fall faster than light ones. This is not true as the speed is dependent on gravity and drag."

swiftcoder, but the movement is based on force, so it will have the desired effect. Velocity of fall will be the same, but pushing the object higher and higher will be more difficult.

Share this post


Link to post
Share on other sites
tastysushifish    106
Yes, felipefdev that's the effect I'm going for, any recommendations or ideas how I would code such a thing, would i be better off basing it off of time in the air or Something else, I'm still a bit of a noob so any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
felipefsdev    360
You know what would be more safe (physics engines tend to not like when you change certain properties like mass, often) than changing the rigid body mass constantly: apply less force the more time the rigid body is in the air.

You could reduce the force that is applied by a certain factor. For example, in each update, you could (pseudocode, since I don't know the particularities of Unity/C#):

object::update()
{
   if self::touchingGround() { // Check if the ground is right bellow
      self.applicableForce = 100.0; // My arbitrary max force
   } else {
      // If not touching ground, reduce applicable force to 75% each frame 
      self.applicableForce = self.applicableForce * 0.75 * deltaTime; // Adjust with the delta time
   }
   // Check input
   if getInput("up") {
      self::applyForce(directionVector * self.applicableForce) // Apply the force in the rigid body (self)
   }
}

Share this post


Link to post
Share on other sites
Norman Barrows    7179

it will have to based on altitude, not time. otherwise they could push it sideways until it would no longer move.


you could just limit the altitude, or make the force a function of altitude.   F effective =  F in / alt. that sort of thing.

Share this post


Link to post
Share on other sites
tastysushifish    106

both good ideas, I'll mess around and see what I can create with your suggestions,

I think I want a hard cutoff so for example... after 5 seconds in the air the player loses all control and falls in whatever direction they were previously accelerating.

 

Thanks again!

Share this post


Link to post
Share on other sites
felipefsdev    360

it will have to based on altitude, not time. otherwise they could push it sideways until it would no longer move.


I was thinking about that, but if the terrain isn't flat, that can be flawed. Imagine if the player is on a mountain: it's at a high altitude, but technically it's still the ground. Using the altitude as parameter and raycast to determine the altitude isn't a good idea either. If there's a cliff, and the character just stepped out of the cliff, the effect will be quite drastic: the altitude will go from 0 to "MAX_ALTITUDE" (whatever the max altitude tested by the raycast is).

Share this post


Link to post
Share on other sites
cmac    275

Using mass to constrain the player to the world sounds like a really bad idea. Why can't you just limit the velocity and not apply forces after a certain theshold?

Share this post


Link to post
Share on other sites
tastysushifish    106
The reason I thought limiting the mass would be interesting was because the force exerted could, at the right setting just bairly push the player up, like imagine a fat bird getting tired after a few flaps he can still fly upwards but only a tiny bit and at a significantly reduced rate. I figured increasing the mass would produce the desired effect. I hope this explanation doesn't make things more confusing.

I'm working on getting some images of my current build so you guys can see what I'm working with.

Share this post


Link to post
Share on other sites
If just being unable to push the player upwards into the stratosphere is the goal, a physically incorrect but probably plausible enough alternative might be a spring constraint to Earth (or a heavy object on the floor). Setting Max Distance to infinity so there's no cutoff, you have an ever-increasing downward force according to Hooke's Law (which will eventually equal/exceed the force that you are able to apply, thus there is an end to it). Or, you could do it with gameplay mechanics. Instead of not allowing the player be pushed into the stratosphere, [i]do allow[/i] that to happen. Only, above some height, show a "breath meter" decreasing, the higher up the faster, and if the player doesn't choose to lower the height any time soon, there's a sudden end to the game.

Share this post


Link to post
Share on other sites
Norman Barrows    7179
Imagine if the player is on a mountain: it's at a high altitude, but technically it's still the ground.

 

by "altitude" i mean distance from the ground below the object to the object. not from sea level or some other arbitrary value, such as y=0 in a left hand 3D system.

 

 

raycast to determine the altitude

 

altitude is usually determined by subtracting the object y from the ground y returned by the heightmap.

Edited by Norman Barrows

Share this post


Link to post
Share on other sites

Imagine if the player is on a mountain: it's at a high altitude, but technically it's still the ground.

by "altitude" i mean distance from the ground below the object to the object. not from sea level or some other arbitrary value, such as y=0 in a left hand 3D system.
Why, though? It's perfectly OK if you have a "high altitude" while standing on a mountain.
The stratosphere starts anywhere from 10km altitude to 18km altitude (depending on whether you're closer to the equator or the poles). A high mountain might be, for example, 8611 or even 8848 meters high, so you are indeed darn close to entering the stratosphere. You need breathing support as well.

So... is it really wrong to consider standing on that mountain that "high up" and prevent the player from going much higher? I think not. I think that's just how it should be.

Share this post


Link to post
Share on other sites
tastysushifish    106

 

"for some context, I have a character who's movement is based off exerting force in a set direction.

 

perhaps a better explanation of the desired gameplay might help. 

 

 

Sure, no problem!

here's a better explanation of the gameplay.

As I mentioned in the original post, the player's movement is based on the force exerted in a set direction.

I made this work by creating a circular ring of trigger colliders that were scripted to apply force in the direction of the

player object when clicked on.  (see image below.) 

gallery_247866_1237_25075.png
Here is a link to a video of the set up working in the Unity editor.
 
 
please let me know if you need me to specify anything else. 
Thanks!

Share this post


Link to post
Share on other sites
felipefsdev    360
Changing the force by reducing it gradually (like i mentioned in my post) or setting the force to 0 after S seconds sounds perfect for this case. Other undesired effect of changing the mass is that it will change the momentum, and will make the collision response between the character and other objects to vary according with the mass of the character.

Share this post


Link to post
Share on other sites
tastysushifish    106

Changing the force by reducing it gradually (like i mentioned in my post) or setting the force to 0 after S seconds sounds perfect for this case. Other undesired effect of changing the mass is that it will change the momentum, and will make the collision response between the character and other objects to vary according with the mass of the character.

 

That seems like the best solution, Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now