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

Started by
17 comments, last by tastysushifish 6 years, 10 months ago
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.
Advertisement

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

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

"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.
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.
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)
   }
}

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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

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!

i'd say altitude too, a weird poor-man's way might be to

density += velocity.z * factor * dt;

at each step.

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

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?

This topic is closed to new replies.

Advertisement